node.js - 如何在firebase云函数中使用await async?

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

我已经在 Nodejs 6 中构建了几个云函数,现在我想使用 await。我怎么做?如何升级到 Node 8?我不想转向 typescript 。

我尝试将 "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个函数。

最佳答案

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

"parserOptions": {
  "ecmaVersion": 6
}

// to

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

关于node.js - 如何在firebase云函数中使用await async?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53384916/

相关文章:

node.js - bcrypr 异步与同步 Node.js

javascript - NodeJS : Append file inside a callback function

node.js - 在不同的 Node 进程之间进行通信的最佳方法是什么?

security - 使用 Firebase 应用 key 对服务器进行身份验证

firebase - 有没有为 flutter 桌面使用 firebase 存储?

firebase - Flutter Firebase 性能 - 未处理的异常

python - `eventlet.spawn` 没有按预期工作

node.js - Nodejs 中的错误处理

node.js - 明确限制请求大小

node.js - 如何在reactjs中渲染swagger.json内容