【问题标题】:mongoose await promise error猫鼬等待承诺错误
【发布时间】: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


【解决方案1】:

我认为你在函数之前没有使用 async 关键字。

试试这个代码。

希望这个答案对你有所帮助。

router.route('/errors')
    .post(async (req, res) => {
        const envirementName = getProjectEnv(getErrorLocation(req.body.error));

        let envCollection = await EnvirementProjectsCollection.findOne({envirementName}).exec();
        console.log(envCollection);
    });

【讨论】:

    猜你喜欢
    • 2017-06-15
    • 1970-01-01
    • 2015-05-12
    • 2021-01-18
    • 1970-01-01
    • 2012-12-14
    • 2019-06-04
    • 2017-08-18
    • 2018-02-15
    相关资源
    最近更新 更多