javascript - 等待循环的 SyntaxError : Unexpected reserved word,

标签 javascript node.js google-cloud-functions

我有这样的功能:

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 loops are first available in node in version 10 .您可能在本地或 package.json 中使用较早的 Node 版本。

您需要确保在用于部署的两个本地 Node 中至少使用 Node 10:

$ node --version
...should print 10.x.x or later

并且还可以在 package.json 中定位 Node 10,以便可以告诉 Cloud Functions 使用哪个版本进行部署:

  "engines": {
    "node": "10"
  }

关于javascript - 等待循环的 SyntaxError : Unexpected reserved word,,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61980704/

相关文章:

node.js - 为什么我在部署 firebase simple 函数时出现此错误?

javascript - SharePoint 2013 - 获取应用程序中JS中所有用户的列表

javascript - 自动将文本内容拆分为偶数列

node.js - 无法在 docker 容器中安装 Node 10.x 版本

node.js - 以编程方式运行 knex 种子 :run

javascript - 有没有办法在创建 Firebase 云函数的同一文件中调用非云函数?

javascript - 使用 CreateJS 单击删除形状

javascript - 使用 Mocha 和 Chai 断言 Javascript 数组中包含的值

javascript - 快速验证器检查输入是否是可用选项之一

python-3.x - GCP Cloud 函数超时