簡體   English   中英

貓鼬等待承諾錯誤

[英]mongoose await promise error

我試圖在查詢集合時使用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()函數將返回我要等待的promise。 目前,我找不到錯誤。 我很樂意為您做些解釋並為我做錯事情提供幫助。

提前致謝!

我認為您尚未在該函數之前使用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);
    });

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM