Ben*_*min 5 mongoose mongodb node.js express
我是一个使用node和mongoose的新手开发人员,并想知道用mongoose链接查询的最佳方法是什么.我在下面做,它不起作用.
User.findByIdAndUpdate(req.params._id, user, { upsert: true })
.exec((err, updatedUser) => {
if (addedCollections) {
return User.findByIdAndUpdate(req.params._id, { $push: { _collections: { $each: addedCollections } } }, { upsert: true }).exec();
}
return new Query;
})
.exec(() => {
return User.findById(req.params._id).populate('_collections');
})
.exec((err, user) => {
res.json({ user });
})
Run Code Online (Sandbox Code Playgroud)
如何链接多个查询?
rob*_*lep 11
您可以使用承诺链,它看起来非常类似于您尝试执行的操作:
User.findByIdAndUpdate(req.params._id, user, { upsert: true })
.then(updatedUser => {
if (addedCollections) {
return User.findByIdAndUpdate(req.params._id, { $push: { _collections: { $each: addedCollections } } }, { upsert: true });
}
})
.then(() => {
return User.findById(req.params._id).populate('_collections');
})
.then(user => {
res.json({ user });
})
.catch(err => {
res.status(500).json({ error : err });
});
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
5661 次 |
| 最近记录: |