I have the following structure in the following tables.
partner (idPartner, name, email, phone) example Tag partner (old id, tag)
What I need to do, a select in the partner table and make a is in the result and query the tableTag partner by the idParceiro and bring the results to fill a Json that stays that way.
[{
nome: 'nome teste',
email: 'email teste',
telefone: 1199999999,
tags: ['tag1', 'tag2', 'tag3']
}, {
nome: 'nome teste',
email: 'email teste',
telefone: 1199999999,
tags: ['tag1', 'tag2', 'tag3']
}]
The problem is that when I query the first table and make a for in the result to get the ID and go in the other table it is lost because it is async.
How can I solve this in NODE.JS because I have several queries that depend on another to generate a Json.
Below is my code.
router.route('/parceiro').get(function(req, res) {
parceiro.consultar(req, function(err, rows){
if(err) return res.status(400).json(err);
var p = [];
_.each(rows, function(one) {
var pa = {};
pa.parceiroId = one.parceiroId;
pa.clienteId = one.clienteId;
pa.nome = one.nome;
pa.imagemDestaque = one.imagemDestaque;
pa.imagemLogo = one.imagemLogo;
pa.desconto = one.desconto;
pa.titulo = one.titulo;
pa.descricao = one.descricao;
pa.urlSite = one.urlSite;
pa.validadeDe = one.validadeDe;
pa.validadeAte = one.validadeAte;
pa.isCupom = one.isCupom;
pa.urlOferta = one.urlOferta;
pa.cupomDesconto = one.cupomDesconto;
pa.ativo = one.ativo;
pa.dataCadastro = one.dataCadastro;
pa.tags = [];
parceiro.tag(req, function(err, r){
_.each(r, function(two) {
pa.tags.push(two.tag);
});
});
pa.categorias = [];
pa.regioes = [];
p.push(pa);
});
return res.status(200).json(p);
});
});