[英]mongoose await promise error
I am trying to use await while querying for a collection but I can't run it. 我试图在查询集合时使用await,但是我无法运行它。 I don't see the mistake 我没看错
router.route('/errors')
.post((req, res) => {
const envirementName = getProjectEnv(getErrorLocation(req.body.error));
let envCollection = await EnvirementProjectsCollection.findOne({envirementName}).exec();
console.log(envCollection);
});
It crashes with - 它崩溃-
let envCollection = await EnvirementProjectsCollection.findOne({envirementName}).exec();
^^^^^^^^^^^^^^^^^^^^^^^^^^^^
SyntaxError: Unexpected identifier
at createScript (vm.js:74:10)
at Object.runInThisContext (vm.js:116:10)
at Module._compile (module.js:588:28)
From what I saw the exec() function of the query will return a promise which I want to await for. 从我看到的查询的exec()函数将返回我要等待的promise。 Currently I can't find the mistake. 目前,我找不到错误。 I would be glad for some explanation and help with what I am doing wrong. 我很乐意为您做些解释并为我做错事情提供帮助。
Thanks in advance! 提前致谢!
I think you haven't used async keyword before the function. 我认为您尚未在该函数之前使用async关键字。
Try with this Code. 尝试使用此代码。
Hope this answer is helpfull to you. 希望这个答案对您有帮助。
router.route('/errors')
.post(async (req, res) => {
const envirementName = getProjectEnv(getErrorLocation(req.body.error));
let envCollection = await EnvirementProjectsCollection.findOne({envirementName}).exec();
console.log(envCollection);
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.