我正在使用快递,这是我的路线功能。此函数从商店表中检索所有商店,然后回调另一个系列函数,该函数循环遍历商店并根据特定的StoreId从另一个表中查找国家。
错误是
router.get('/get-data', function(req, res, next) {
var stores = [];
async.series([
function(callback){
// running mysql query here, then assigning record to stores
stores = record;
callback();
},
function(callback)
{
async.forEachOf(stores, function(value, key, callback) {
var Store = stores[key].StoreId;
Mysql.query(query + Store)
.then(myCallback(key))
.catch(function(err)
{
return res.send({'success': false});
});
})
callback();
function myCallback(index)
{
return function(record)
{
console.log(index); //okhere
if(record)
{
stores[index]['countries'] = record;
console.log(stores); // store has countires
}
}
}
},
function(callback)
{
return res.send({'success': stores}); //return store without countries
},
]);
});发布于 2018-02-13 17:08:34
这是因为值存储在闭包中。当调用回调function(record) {...}时,aa的值已经是3(当异步调用没有返回时,它会递增)。所以为了纠正这个问题,使用包装这个函数的另一个函数并调用那个函数。例如,
Mysql.query(query + Store)
.then(myCallback(aa))
// Function that returns a function that'll
function myCallback(currentAa) {
return function(record){
if(record) {
console.log(currentAa);
}
}
}https://stackoverflow.com/questions/48762234
复制相似问题