【发布时间】:2018-06-12 15:32:25
【问题描述】:
我在查询集合时尝试使用 await,但我无法运行它。我没有看到错误
router.route('/errors')
.post((req, res) => {
const envirementName = getProjectEnv(getErrorLocation(req.body.error));
let envCollection = await EnvirementProjectsCollection.findOne({envirementName}).exec();
console.log(envCollection);
});
它崩溃了 -
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)
从我看到的查询的 exec() 函数将返回一个我想要等待的承诺。目前我找不到错误。对于我做错的事情,我会很高兴得到一些解释和帮助。
提前致谢!
【问题讨论】:
-
await 只能在
async函数中使用 -
您的语法无效。你有一个尾随
)。 -
@CertainPerformance 我在复制错误时对其进行了编辑,可能是拼写错误。
-
maybe a typo是或不是 - 检查您的原始代码。
标签: javascript express mongoose