The problem encountered by the poster should be that he performed an asynchronous data query, and at the same time, the second query relied on the results of the first query . Let’s just look at the code!
var sql = 'xxxxxxx';
async.waterfall([function (cb) {
mysql.connection.query(sql, function (err, result) {
if (err) {
console.log(err);
throw err;
}
var response = [];
//此处省略数据处理逻辑
cb(null, response);
});
}, function (result, cb) {
async.eachSeries(result, function iterator(item, callback) {
var sql = 'x'x'x'x'x'x'x'x'x'x';
mysql.connection.query(sql, function (err, commentResult) {
if (err) {
console.log(err);
throw err;
}
var formatResult = [];
_.each(commentResult, function (item) {
// 省略处理逻辑
formatResult.push(item);
});
if (commentResult.length) {
//xxxxx 省略处理逻辑
}
callback();
});
}, function done(err) {
console.log("err: " + err);
cb(null, result);
});
}], function (err, results) {
cb(null, {
"msg": "查询成功",
"result": 1,
"data": results
});
});
First define two different query methods:
You can try this method, I think it can meet your requirements
The problem encountered by the poster should be that he performed an asynchronous data query, and at the same time, the second query relied on the results of the first query . Let’s just look at the code!
The above code mainly uses async middleware
Save the content queried at each step in the same object, and then render it out. It’s very simple!!