node-mysql 获取值问题?(已解决)
发布于 9 年前 作者 liuxufei 5452 次预览 最后一次回复是 9 年前 来自 问答
示例代码:
pool.getConnection(function(err, connection) {
async.waterfall([function (cb) {
connection.query('SELECT * FROM aaa', function(err, res_aaa) {
cb(err, res_aaa);
});
}, function (res_aaa, cb) {
for (let i = 0, l = res_aaa.length; i < l; i++) {
connection.query('SELECT name FROM bbb WHERE id = ?', [user_id], function(err, name) {
res_aaa[i].name = name; // name 如何才能传到外部 res_aaa?
});
}
console.log(res_aaa); // 这里如何获取上面 name 的值?
cb(err, res_aaa);
}], function (err, result) {
});
});
console.log(res_aaa); 这里的 res_aaa 怎么才能得到 name 的值?
8 回复
把
console.log(res_aaa);放到下一个function里面去。@alsotang
@alsotang 还是没找到方法
https://github.com/alsotang/node-lessons/tree/master/lesson4 看看这个吧。换用 eventproxy 方便点
这一段用async.forEachOf改写,形如:
补充一个sql执行的封装。
以后需要执行的时候就这样子:
@alsotang 谢谢,eventproxy 的方式已会使用。你很喜欢用 eventproxy 胜过其他方式啊,cnode 也是用这个
@welchwsy 谢谢,通过 async.forEachOf 也实现了。