即使在推送数据后,节点 js 也得到空白对象
Node js getting blank obj even after pushing data
这里我试图将数据推入我的数组,但它总是空的。
read_file: ['pass_fileData', function (result, cb) {
let obj = [];
async.each(result.pass_fileData, function (item) {
knex
.select('xxxxx')
.from('xxxx')
.innerJoin('xxxx', 'xxxx', 'xxx')
.where('xxxxx', '=', item)
.then(function (data) {
obj.push(data) // here I am pushing data to array
})
.catch(function (err) {
cb(err);
})
})
cb(null, obj)
}]
在 CB(null, obj)
中,我没有得到任何数据,但是当我控制台时,我从数据库中得到数据。
因为你的函数是异步的。这意味着当您的回调 cb(null, obj)
被调用时,数据还不存在。您想在执行每个异步函数后调用回调。
async.each
可以接受第三个参数,这是一个回调函数,一旦函数完成就会被调用。
您的代码应如下所示:
read_file: ['pass_fileData', function (result, cb) {
let obj = [];
async.each(result.pass_fileData, function (item, callback) {
knex
.select('xxxxx')
.from('xxxx')
.innerJoin('xxxx', 'xxxx', 'xxx')
.where('xxxxx', '=', item)
.then(function (data) {
obj.push(data) // here I am pushing data to array
callback() // Iteratee callback
})
.catch(function (err) {
callback(err); // Iteratee callback
})
}, function (err) { // end callback
cb(err, obj) // Your callback that takes the obj
})
}]
问题是你在另一个异步调用(async.each
)中有一个异步调用(knex
),cb(null, obj)
没有等待上一个异步任务完成因此执行更早。另外,如果 async.each
不是必需的,你可以去掉它,只使用 Promises。只需遍历 result.pass_fileData
,将所有 knex
promise 存储在数组中,然后将 Promise.all
与数组一起使用即可完成工作。
read_file: ['pass_fileData', function (result, cb) {
const obj = [];
const promises = [];
// asuming "result.pass_fileData" is an array
result.pass_fileData.forEach(function (item) {
const singlePromise = knex
.select('xxxxx')
.from('xxxx')
.innerJoin('xxxx', 'xxxx', 'xxx')
.where('xxxxx', '=', item)
.then(function (data) {
obj.push(data) // here I am pushing data to array
})
.catch(function (err) {
cb(err);
});
promises.push(singlePromise); // store all the promises in an array
});
Promises.all(promises).then(function() {
cb(null, obj);
});
}]
如果你可以使用 async/await
(为什么不呢?)你可以稍微更改一下代码
read_file: ['pass_fileData', async function (result, cb) {
const obj = [];
const promises = [];
// asuming "result.pass_fileData" is an array
result.pass_fileData.forEach(function (item) {
const singlePromise = knex
.select('xxxxx')
.from('xxxx')
.innerJoin('xxxx', 'xxxx', 'xxx')
.where('xxxxx', '=', item)
.then(function (data) {
obj.push(data) // here I am pushing data to array
})
.catch(function (err) {
cb(err);
});
promises.push(singlePromise); // store all the promises in an array
});
await Promises.all(promises);
cb(null, obj);
}]
注意async function (result, cb)...
前面的async
关键字和await Promises.all(promises);
前面的await
关键字
希望对您有所帮助
这里我试图将数据推入我的数组,但它总是空的。
read_file: ['pass_fileData', function (result, cb) {
let obj = [];
async.each(result.pass_fileData, function (item) {
knex
.select('xxxxx')
.from('xxxx')
.innerJoin('xxxx', 'xxxx', 'xxx')
.where('xxxxx', '=', item)
.then(function (data) {
obj.push(data) // here I am pushing data to array
})
.catch(function (err) {
cb(err);
})
})
cb(null, obj)
}]
在 CB(null, obj)
中,我没有得到任何数据,但是当我控制台时,我从数据库中得到数据。
因为你的函数是异步的。这意味着当您的回调 cb(null, obj)
被调用时,数据还不存在。您想在执行每个异步函数后调用回调。
async.each
可以接受第三个参数,这是一个回调函数,一旦函数完成就会被调用。
您的代码应如下所示:
read_file: ['pass_fileData', function (result, cb) {
let obj = [];
async.each(result.pass_fileData, function (item, callback) {
knex
.select('xxxxx')
.from('xxxx')
.innerJoin('xxxx', 'xxxx', 'xxx')
.where('xxxxx', '=', item)
.then(function (data) {
obj.push(data) // here I am pushing data to array
callback() // Iteratee callback
})
.catch(function (err) {
callback(err); // Iteratee callback
})
}, function (err) { // end callback
cb(err, obj) // Your callback that takes the obj
})
}]
问题是你在另一个异步调用(async.each
)中有一个异步调用(knex
),cb(null, obj)
没有等待上一个异步任务完成因此执行更早。另外,如果 async.each
不是必需的,你可以去掉它,只使用 Promises。只需遍历 result.pass_fileData
,将所有 knex
promise 存储在数组中,然后将 Promise.all
与数组一起使用即可完成工作。
read_file: ['pass_fileData', function (result, cb) {
const obj = [];
const promises = [];
// asuming "result.pass_fileData" is an array
result.pass_fileData.forEach(function (item) {
const singlePromise = knex
.select('xxxxx')
.from('xxxx')
.innerJoin('xxxx', 'xxxx', 'xxx')
.where('xxxxx', '=', item)
.then(function (data) {
obj.push(data) // here I am pushing data to array
})
.catch(function (err) {
cb(err);
});
promises.push(singlePromise); // store all the promises in an array
});
Promises.all(promises).then(function() {
cb(null, obj);
});
}]
如果你可以使用 async/await
(为什么不呢?)你可以稍微更改一下代码
read_file: ['pass_fileData', async function (result, cb) {
const obj = [];
const promises = [];
// asuming "result.pass_fileData" is an array
result.pass_fileData.forEach(function (item) {
const singlePromise = knex
.select('xxxxx')
.from('xxxx')
.innerJoin('xxxx', 'xxxx', 'xxx')
.where('xxxxx', '=', item)
.then(function (data) {
obj.push(data) // here I am pushing data to array
})
.catch(function (err) {
cb(err);
});
promises.push(singlePromise); // store all the promises in an array
});
await Promises.all(promises);
cb(null, obj);
}]
注意async function (result, cb)...
前面的async
关键字和await Promises.all(promises);
await
关键字
希望对您有所帮助