javascript - SyntaxError:意外的保留字,等待循环
问题描述
我有这样的功能:
exports.deleteUser = functions.https.onCall(async(data, context) => {
let id = context.auth.uid;
console.log('Delete user: ' + id);
//delete from algolia
usersIndex.deleteObject(id);
console.log(id + 'Deleted from algolia');
//delete user following
await admin.firestore().collection('users').doc(id).collection('Following').get()
.then(async(snapshot) => {
for await (const document of snapshot.docs) {
await admin.firestore().collection('users').doc(document.documentId)
.update({
'NumberOfFollowers': FieldValue.increment(-1)
});
await admin.firestore().collection('users').doc(document.documentId).collection('Followers')
.doc(id).delete();
}
return console.log('Following of ' + id + ' deleted');
});
...
但是当我尝试将其部署到 firebase 功能时,我收到以下错误:
! functions[deleteUser(us-central1)]: Deployment error.
Function failed on loading user code. Error message: Code in file index.js can't be loaded.
Is there a syntax error in your code?
Detailed stack trace: /srv/index.js:47
for await (const document of snapshot.docs) {
^^^^^
SyntaxError: Unexpected reserved word
at createScript (vm.js:80:10)
at Object.runInThisContext (vm.js:139:10)
at Module._compile (module.js:617:28)
at Object.Module._extensions..js (module.js:664:10)
at Module.load (module.js:566:32)
at tryModuleLoad (module.js:506:12)
at Function.Module._load (module.js:498:3)
at Module.require (module.js:597:17)
at require (internal/module.js:11:18)
at getUserFunction (/worker/worker.js:439:24)
发现这个:https ://github.com/nodejs/node/issues/21617但我确实将我的 ForLoop 设置为异步函数......
解决方案
for-await 循环首先在 node 版本 10 中可用。您可能在本地或 package.json 中使用较早的节点版本。
您需要确保在用于部署的两个本地节点中至少使用节点 10:
$ node --version
...should print 10.x.x or later
并且还以 package.json 中的节点 10 为目标,因此可以告诉 Cloud Functions 使用哪个版本进行部署:
"engines": {
"node": "10"
}
推荐阅读
- python - 在 django web API、视图或序列化程序中的何处放置额外的逻辑?
- git - 无法使用原子文本编辑器编写提交消息
- makefile - LiME 内核模块交叉编译失败
- javascript - 推送通知不适用于“/”以外的任何范围
- r - 如何从R中数据框中的未配对元素按组折叠值
- php - Codeigniter 2 - 表单验证自定义规则
- flutter - 在颤振应用程序中添加自定义字体的困惑
- sql - 需要找到该期间/序列中记录的最小日期
- android-studio - 无论如何都要像浏览器检查那样检查计算API调用时间吗?
- android - 如何根据他的邮件或其他 id 在实时数据库中获取其他用户的 firebase id?