I'm trying to export some legacy data from a mysql db as JSON using express and node-mysql. The SQL below works fine. I'm struggling with a simple way to join the 'result' of getOwnerID and to the data for each row returned in compVouchers.
I'm also using async.js having followed another thread, though I'm not sure this is helping. But if I can get away with not using this that might be better.
//join some tables to get comprehensive voucher data
exports.compVouchers = function(req, res) {
var advertType = '"discount_voucher_all_CANCELLED"';
if (connection) {
connection.query('SELECT V.id AS voucher_id, V.title, V.description, V.discount, V.customers_total, V.advert_type, ' +
'V.customers_redeemed, V.start_date, V.expiry_date, V.redemption_code, ' +
'K.image, G.latitude, G.longitude FROM '+dbname+'.vouchers AS V ' +
'LEFT JOIN '+dbname+'.iag_key_tags AS K ON ( V.id = K.id ) ' +
'LEFT JOIN '+dbname+'.iag_geo_tags AS G ON ( V.id = G.id ) ' +
'WHERE V.advert_type like '+advertType , function(err, rows, fields) {
if (err) throw err;
console.log("Got "+rows.length+" Vouchers:");
// now get each vouchers owner id
async.map(rows, getOwnerID, function(err, results){
res.writeHead(200, {'Content-Type': 'text/plain'});
res.end(JSON.stringify(results));
res.end();
});
});
}
};
function getOwnerID(voucher, callback) {
connection.query('SELECT parent_tagid AS owner_id FROM '+dbname+'.iag_key_tag_relationships WHERE TYPE =2 AND tagid = '+ voucher.voucher_id, function(err, info) {
if(err) {
console.log(err);
return callback(err);
}
else {
return callback(null, info);
}
});
}
so
res.end(JSON.stringify(results)); // prints all the owner_id of each voucher only
res.end(JSON.stringify(rows)); // prints the data for each voucher but not the owner_id
Combining node-mysql result rows into single JSON return for node.js doesn't solve the problem but as you can see I have tried to follow the suggestion in that thread.