0
This is the action that is activated by loading the page: In it I get the following array below. My problem is, when there is no record the ARRAY returns empty, I cannot pass a parameter to INPUT.
Ex.: input(value="#{date[0][0]. feed || 0}")
If you do not find the record fill in with zero, but before that you already have an error because you do not find the data parameter[0][0]. feed and consequently does not load the page.
In BD, I tried to set the fields as default NULL and default 0 and still do not return the fields.
If I enter data manually, it works perfectly.
**[ [],** [{"nNomes":0,"salariosMembros":null}], [{"salarioTitular":null}] ]
getDespesa: function(req,res){
var _id = req.params.id;
db.query('SELECT * FROM despesas WHERE idCliente = ?;
SELECT COUNT(nomeCompleto) as nNomes, SUM(ultimosalario) as salariosMembros FROM membros WHERE idCliente = ?;
SELECT ultimoSalario as salarioTitular FROM cadastro WHERE idCliente = ?', [_id, _id, _id], function(err, result){
if(err) throw err;
var results = JSON.parse(JSON.stringify(result));
console.log(results);
var soma = (results[1][0].salariosMembros + results[2][0].salarioTitular) * 100;
console.log(soma);
var data1 = results[0][0];
console.log(data1);
res.render('titularDespesas', { id: _id, data: result });
result = results;
return result;
});
},
After researching a little more I managed to get a result:
I don’t know if it’s the right way, I know you’ve answered my purpose:
var data = {
alimentacao : 0,
agua : 0,
luz : 0,
iptu : 0,
telefone : 0,
mensalidadesEscolares : 0,
transporte : 0,
assistenciaMedica : 0,
medicamentosContinuos : 0,
totalDespesas : 0,
rendaBruta : 0,
rendaLiquida : 0,
nMembros : 0,
rendaPerCapita : 0
}
var valor;
if(**results[0]** == ''){
valor = data;
}else{
valor = results[0][0];
}
Personal vlw!
What is
input(value="#{data[0][0].alimentacao || 0}")
? Using EJS as engine?– BrTkCa
What is the need for
JSON.parse(JSON.stringify(result));
?– relaxeaza
@Lucascosta I’m using Express and Jade
– Marcos Paulo Cunha Martins
@RafaelMafra: Sem o JSON.parse(JSON.stringify(result)); os arrays retornam com uma configuração diferente, assim:
[ [], [ RowDataPacket { nNomes: 0, salariosMembros: null } ], [ RowDataPacket { salarioTitular: null } ] ]
– Marcos Paulo Cunha Martins
I believe one option is to reach JADE if the property exists, more or less: http://stackoverflow.com/questions/5070841/jade-template-engine-how-to-check-if-a-variable-exists
– BrTkCa
@Lucascosta, the big problem is selecting the property of the object. That’s what I’m not getting. This form you showed worked if I use it as follows: date[0][0] because there are the 0. arrays However, there are several input fields and each one with its data. In this case, I need to access as follows: date[0][0]. feed, date[0][0]. water and etc. Only that the array remains empty, there is no feed property, then error saying that the feed property is undefined.
– Marcos Paulo Cunha Martins