我已经在 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/