如何在Firebase云功能中使用等待异步?

时间:2018-11-20 01:21:32

标签: node.js firebase asynchronous async-await google-cloud-functions

我已经在nodejs 6中构建了几个云函数,现在我想使用await。我怎么做?如何升级到节点8?我不想转向打字稿。

我尝试将"engines": {"node": "8"}添加到package.json,但它什么都没有改变。

这是我做的测试云功能:

exports.awaittest = functions.https.onRequest(async (req, res)=>{
    ref = await admin.database().ref("/something/");
    res.send(ref)
})

当我尝试部署它(编辑package.json)时,我得到了:

SyntaxError: Unexpected token (
    at createScript (vm.js:56:10)
    at Object.runInThisContext (vm.js:97:10)
    at Module._compile (module.js:549:28)
    at Object.Module._extensions..js (module.js:586:10)
    at Module.load (module.js:494:32)
    at tryModuleLoad (module.js:453:12)
    at Function.Module._load (module.js:445:3)
    at Module.require (module.js:504:17)
    at require (internal/module.js:20:19)
    at getUserFunction (/var/tmp/worker/worker.js:413:24)
!  functions[awaittest]: Deployment error.
Function load error: Code in file index.js can't be loaded.
Is there a syntax error in your code?
Detailed stack trace: /user_code/index.js:59
exports.awaittest = functions.https.onRequest(async (req, res)=>{
                                                    ^

如何升级到node8?我已经在node6中部署了6个功能。

1 个答案:

答案 0 :(得分:1)

我通过更改以下内容在functions/.eslintrc.json目录中解决了该问题:

"parserOptions": {
  "ecmaVersion": 6
}

// to

"parserOptions": {
  "ecmaVersion": 2017
}
'''