I am having a trouble with mongoose used with node.js. In my model I am having only group supervisor's ID, and in this route I want also to add supervisorName to variable (this name is stored in Group model). So, yeah, I've read some about promises, but still, I dont have any idea how to solve that. (Basically, I just want to loop through all groups, get their models from mongodb, and assign supervisor name to every group)
我遇到了与node.js一起使用的mongoose的问题。在我的模型中,我只有组主管的ID,并且在此路由中我还想将supervisorName添加到变量(此名称存储在组模型中)。所以,是的,我已经阅读了一些关于承诺的内容,但是,我仍然不知道如何解决这个问题。 (基本上,我只是想遍历所有组,从mongodb获取他们的模型,并为每个组分配主管名称)
router.get('/manage', function(req, res, next) {
Group.find({}, function(err, groups) {
groups.forEach(function(group) {
Group.findById(group.supervisor, function(err, supervisor) {
group.supervisorName = supervisor.name;
console.log(supervisor.name);
});
});
}).then(function() {
res.render('groups/groups_manage', {groups : groups});
});
});
1 个解决方案
#1
0
You can map
your groups array into array of promises
and use Promise.all
to resolve them all.
您可以将groups数组映射到promises数组,并使用Promise.all解决所有问题。
router.get('/manage', function(req, res, next) {
Group.find({})
.exec() // 1
.then(groups => {
return Promise.all(groups.map(group => { // 2, 3
return Group.findById(group.supervisor)
.exec()
.then((supervisor) => { // 4
group.supervisorName = supervisor.name;
return group;
});
}));
})
.then(propGroups => {
res.render('groups/groups_manage', {groups : popGroups});
});
});
Explanations:
- Mongoose Model methods supports both callbacks and promises interface. However to use the promise interface we need to use the
exec
method and remove the callbacks. - Mongoose Model方法支持回调和promises接口。但是要使用promise接口,我们需要使用exec方法并删除回调。
-
Promise.all
takes an array of promises. It waits for all of them to resolve and any of them to reject (give error). Note: The promises are executed in parallel - Promise.all采取了一系列承诺。它等待所有人解决,其中任何一个拒绝(给出错误)。注意:承诺是并行执行的
- We have an array of
group
objects, we map them into promises. Calling.findById
and again usingexec
to get a promise from it. Note: Remember to return your promises - 我们有一组组对象,我们将它们映射到promises中。调用.findById并再次使用exec来获取它的承诺。注意:请记住退还您的承诺
- We update the object, and return it as the final result of promise.
- 我们更新对象,并将其作为promise的最终结果返回。
#1
0
You can map
your groups array into array of promises
and use Promise.all
to resolve them all.
您可以将groups数组映射到promises数组,并使用Promise.all解决所有问题。
router.get('/manage', function(req, res, next) {
Group.find({})
.exec() // 1
.then(groups => {
return Promise.all(groups.map(group => { // 2, 3
return Group.findById(group.supervisor)
.exec()
.then((supervisor) => { // 4
group.supervisorName = supervisor.name;
return group;
});
}));
})
.then(propGroups => {
res.render('groups/groups_manage', {groups : popGroups});
});
});
Explanations:
- Mongoose Model methods supports both callbacks and promises interface. However to use the promise interface we need to use the
exec
method and remove the callbacks. - Mongoose Model方法支持回调和promises接口。但是要使用promise接口,我们需要使用exec方法并删除回调。
-
Promise.all
takes an array of promises. It waits for all of them to resolve and any of them to reject (give error). Note: The promises are executed in parallel - Promise.all采取了一系列承诺。它等待所有人解决,其中任何一个拒绝(给出错误)。注意:承诺是并行执行的
- We have an array of
group
objects, we map them into promises. Calling.findById
and again usingexec
to get a promise from it. Note: Remember to return your promises - 我们有一组组对象,我们将它们映射到promises中。调用.findById并再次使用exec来获取它的承诺。注意:请记住退还您的承诺
- We update the object, and return it as the final result of promise.
- 我们更新对象,并将其作为promise的最终结果返回。