等到mongoDB查询中的所有项目都处理完毕后,再继续 [英] Wait till all items from mongoDB query are processed before continuing
问题描述
我正在一个非常复杂的项目中,必须处理许多mongoDB查询才能显示数据.现在,当对大量数据执行查询时,页面加载后并不会立即显示所有数据.现在,在使用next()
函数之前,我通过设置超时函数解决了该问题.
I'm working on a pretty complex project where a lot of mongoDB queries have to be processed in order to show data. Right now, when performing a query over a big set of data, not all data shows as soon as the page loads. For now I fixed that problem by setting a timeout function, before using the next()
function.
因此这是其中一个函数的调用位置:
So this is where one of the functions is called:
router.get('/playwall', account.checkSession, playwalls.get, function(req, res, next) {
res.render('dashboard/playwalls/index');
});
在检查用户是否登录后,将触发playwalls.get
函数.该功能现在的工作方式如下:
After checking if the user is logged in, the playwalls.get
function fires. This is how that function works right now:
const playwalls = {
/* GET ALL PLAYWALLS FOR THAT USER, FOR ADMIN GET THEM ALL
-------------------------------------------------------------- */
get(req, res, next) {
req.session.playwalls = [];
const dataCollection = db.collection('data');
const data = dataCollection.find({});
console.log(typeof data)
const today = new Date();
data.forEach(function(d) {
console.log(data.length)
if(d.admin == req.session.userData.username || req.session.userData.role == 'admin') {
const expireDate = new Date(d.date);
if(expireDate < today && d.status == 'actief') {
playwalls.editStatus(d, req, res, next);
}
req.session.playwalls.push(d);
}
})
setTimeout(function() {
next();
}, 1000)
},
// Here the rest of the methods
}
我的问题是;如何确保仅在data.forEach
循环已处理查询返回的所有文档时才触发next()
函数?这样我就可以摆脱setTimeout()
函数.
My question is; how can I make sure that the next()
function is only fired if the data.forEach
loop has processed all the documents that are returned by the query? So I can get rid of the setTimeout()
function.
希望我的问题很明确,有人可以帮助我.我在互联网上四处张望,但找不到正确的答案.
Hope my question is clear and someone can help me. I've looked around on the internet but couldn't find a proper answer.
提前谢谢!
推荐答案
You should use Cursor.toArray, so you can iterate over your documents in sync way, then if you need to do some async job inside forEach
callback you can use Promise
's
const playwalls = {
/* GET ALL PLAYWALLS FOR THAT USER, FOR ADMIN GET THEM ALL
-------------------------------------------------------------- */
get(req, res, next) {
req.session.playwalls = [];
const dataCollection = db.collection('data');
const data = dataCollection.find({});
console.log(typeof data)
const today = new Date();
data.toArray(function (err, documents) {
if (err) {
return next(err);
}
documents.forEach(function (d) {
console.log(data.length)
if (d.admin == req.session.userData.username || req.session.userData.role == 'admin') {
const expireDate = new Date(d.date);
if (expireDate < today && d.status == 'actief') {
playwalls.editStatus(d, req, res, next);
}
req.session.playwalls.push(d);
}
})
next();
})
}, // Here the rest of the methods
}
这篇关于等到mongoDB查询中的所有项目都处理完毕后,再继续的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!