我尝试使用express和node-mysql将一些遗留数据从mysql数据库导出为JSON。下面的SQL工作正常。我正在努力用一种简单的方式来加入“结果”#39; getOwnerID和compVouchers中返回的每一行的数据。
我也使用async.js跟随另一个帖子,虽然我不确定这有帮助。但是,如果我可以逃避不使用它可能会更好。
//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);
}
});
}
所以
res.end(JSON.stringify(结果)); //仅打印每张优惠券的所有owner_id
res.end(JSON.stringify(行)); //打印每个凭证的数据,但不打印owner_id
Combining node-mysql result rows into single JSON return for node.js无法解决问题,但正如您所见,我已尝试按照该主题中的建议行事。
答案 0 :(得分:1)
这里有一些美感而不是在演出:) 试试这个:
var result={}, c=rows.length;
function getOwnerID(voucher, cb){
connection.query('SELECT ...', function(err, info) {
if(err) console.log(err);
else result[info] = voucher;
if(!--c)return cb();
});
}
while(rows.length){
getOwnerID(rows.pop(), function(){
res.writeHead(200, {'Content-Type': 'text/plain'});
res.end(JSON.stringify(results));// {"owner1":{voucher1}, "owner2":{voucher2}}
res.end();
})
}
答案 1 :(得分:0)
好的伙计们(感谢@vp_arth在一个让我接近的有趣方向上轻推我,错字结果应该是btw的结果)
所以,无论如何,我最终得到了一个黑客解决方案,我使用.push underscore.js和.replace来帮助我清理JSON数据,这样我就可以在像MongoDB这样的nosql数据库中使用它。 。
//declare global array variable... there must be a more elegant solution
var compV = [];
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;
// now get each vouchers owner id
console.log("Got "+rows.length+" Vouchers:");
async.each(rows, getOwnerID, function(err, results){
res.writeHead(200, {'Content-Type': 'text/plain'});
// now user underscore.js to clean up JSON
var finalComp = JSON.stringify(un.flatten(un.compact(compV)));
// finally use replace to customise the known output to merging the voucher and owner into single JSON documents
var finalComp2 = finalComp.replace(/},{"owner_id/g,',"owner_id'); //not happy with this but it works
res.write(finalComp2);
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, owner) {
if(err) {
console.log(err);
return callback(err);
}
else {
var arr = [];
arr.push(voucher);
arr.push(owner);
compV.push(arr); //append to global array variable
return callback(null, compV); // doesn't return anything??
}
});
}
也许有更优雅的合并方式
[{" F1_field1":" F1_value1"" F1_field2":" F1_value2"},{" F2_field1&#34 ;:" F2_value2"}]
进入
[{" F1_field1":" F1_value1"" F1_field2":" F1_value2"" F2_field1&#34 ;: " F2_value2"}]
这是我的评论/想法的最终代码
你现在还需要npm安装下划线添加到异步并在变量中声明它们...更不用说node-mysql和express ...我已经使用了" un"而不是" _"所以我不会对以后可能看起来像jquery的代码感到困惑。