Firstly, I am new to nodejs. Working on an API where I have to do multiple queries inside an each loop. So, I started using async module with express. The exact code that I am using is
new_or_updated = {};
async.each(rows, function(e, callbackParent) {
new_or_updated.id = e.id;
async.parallel([
function(callback){
db.query("SELECT sender_string FROM filter_positive_rules_sender WHERE rule_id = ? AND is_deleted = 0", e.id,function(err, rows, fields){
if (err) {
return callback(err);
}
new_or_updated.sender = rows.map(x => x.sender_string);
callback(null);
});
},
function(callback){
db.query("SELECT subject_string FROM filter_positive_rules_subject WHERE rule_id = ? AND is_deleted = 0", e.id ,function(err, rows, fields){
if (err) {
return callback(err);
}
new_or_updated.subject = rows.map(x => x.subject_string);
callback(null);
});
},
function(callback){
db.query("SELECT body_string FROM filter_positive_rules_body WHERE rule_id = ? AND is_deleted = 0", e.id ,function(err, rows, fields){
if (err) {
return callback(err);
}
new_or_updated.body = rows.map(x => x.body_string);
callback(null);
});
}
],
function(err) {
if (err) {
res.status(500).json(
"Internal server error"
).send();
}
console.log(new_or_updated):
callbackParent(null, new_or_updated);
});
},
function(err, result) {
if (err) {
res.status(500).json(
"Internal server error"
).send();
}
console.log(result)
});
As you can see I am trying to populate an object using the async module. When I console new_or_updated in the parallel async, the array is properly built. However, when I send the variable to the each async callback and console it (result variable) I get undefined. Also, when I console new_or_updated in the parentCallback, I only get one element in the array(the last array build using each async module).
What am I doing wrong here? Could someone explain to me how nested async is actually supposed to work in practice.