Problem with empty Array with mysql and nodejs

Asked

Viewed 191 times

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?

  • What is the need for JSON.parse(JSON.stringify(result));?

  • @Lucascosta I’m using Express and Jade

  • @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 } ] ]

  • 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

  • @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.

Show 1 more comment

1 answer

1

It is the responsibility of the programmer to check if the data he wants to access exists. Using if or some other structure to confer this.

  • Actually, after researching a little more I checked that I was trying to search for data beyond the permitted, so I could not structure any condition. I don’t know if it’s the right way, I know it met my purpose: var data = { power : 0, water : 0, light : 0, property : 0, phone : 0, tuition fees : 0, transportation : 0, assistance : 0, medicationContinuos : 0, totalDespesas : 0, rentaBruta : 0, rentLiquida : 0, nMembers : 0, rentPerCapita : 0 } var value; if(Results[0] == '') value = data; value = Results[0][0];

  • Personal thank you!!!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.