I have some problemos with managing the async funciontions. Here is my code:
var query = client.query("select * from usuario");
query.on('row', function(user) {
var queryInterest = client.query("select interes.nombre from interes inner join relacioniu on relacioniu.idinteres=interes.id where relacioniu.idusuario = '"+user.id+"'");
queryInterest.on('row',function(row) {
user.interest = row;
console.log("user added");
});
users.push(user);
});
// After all data is returned, close connection and return results
query.on('end', function() {
done();
console.log("finish");
return res.json(users);
});
The users array push an user before the execution of queryInterest, so the user's interests are never added. How can I solve this?
Thank you so much
users.push(user);
insidequeryInterest.on
. this will solve your problem.query
object. so as soon as the query completes it return the object. you can consider usingasync
library.