Having two parallel mysql queries in node js is meant to retrieve data more efficiently. The following javascript code reads from a mysql database and stores the results in a javascript object:
async.parallel({
aaChannelsCount: function(cb) {
/* GET - channels */
pool.query("SELECT JSON_LENGTH(sort) AS channels FROM channels WHERE bouquet='[\"?\"]'",[rows[0].bouquet], function (err, rows, fields) {
/* CHANNELS - count */
if (rows.length) {
data.push( {"channels": rows[0].channels, "radio": 0} );
};
});
},
aaVideoClub: function(cb) {
/* GET - videoclub */
pool.query("SELECT count(id) AS videoclub FROM vod", function (err, rows, fields) {
/* VIDEOCLUB - count */
data.push( {"videoclub": rows[0].videoclub} );
res.json(data);
});
}
}, function(err, results) {
if (err) {
/* SHOW - unauthorized */
res.sendStatus(401);
} else {
res.json(data);
}
}
);
The current output obtained using res.json(data) is as follows:
[{"channels":3,"radio":0},{"videoclub":2}]
However, the desired output is like this:
{"channels":3,"radio":0, "videoclub":2}
In the code, data.push is used to add results from mysql into a javascript array object, which is then converted into json. However, the desired output has not been achieved yet.