Problem with empty Array with mysql and nodejs

0

This is the action that is activated when loading the page: In it I get the following array as result. My problem is, when I have no records the ARRAY returns empty, I can not pass a parameter to the INPUT.

Ex: input (value="# {data [0] [0] .feedback || 0}")

If it does not find the record it fills with zero, but before that it already presents error by not finding the data parameter [0] [0] .feed and consequently does not load the page.

In BD, I already tried to set the fields to default NULL and default 0 and even then does 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 a little more research, I was able to get a result:

I do not know if it's the right way, I know it has served my purposes:

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!

    
asked by anonymous 23.12.2016 / 14:42

1 answer

1

It is the developer's responsibility to check if the data he or she wants to access exists. Using if or some other structure to check it.

    
23.12.2016 / 17:43