admin管理员组

文章数量:1278853

I have a function like this:

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');
      });
...

but when I try to deploy it to firebase functions I got the following error:

!  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._pile (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)

Found this: but I did set my ForLoop in an async function...

I have a function like this:

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');
      });
...

but when I try to deploy it to firebase functions I got the following error:

!  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._pile (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)

Found this: https://github./nodejs/node/issues/21617 but I did set my ForLoop in an async function...

Share Improve this question edited May 24, 2020 at 1:56 user11141611 asked May 24, 2020 at 1:49 MilvintsissMilvintsiss 1,4482 gold badges20 silver badges35 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 9

for-await loops are first available in node in version 10. You're probably using an earlier node version either locally or in package.json.

You will need to make sure that you are using at least node 10 in both your local node that you use for deployment:

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

and also target node 10 in your package.json so Cloud Functions can be told which version to use for deployment:

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

Use below mands to crack the solution

nvm use lists //if nvm is installed it is useful to check versions of node

node -v // to check which version is getting used

If version of node is below 10.x.x than with the help of nvm we can point to other above 10.x.x using below mand:

nvm use 12.19.0 //in my case, I have option for latest one as it was installed using nvm install 12.19.0

Note: Install nvm for easy switching of node versions

本文标签: javascriptSyntaxError Unexpected reserved wordfor await loopStack Overflow