4

I am using the newest version of Node, v8.9.1, but I get this error when deploying the code below:

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:550
  async function deleteQueryBatch(db, query, batchSize, results) {
        ^^^^^^^^

SyntaxError: Unexpected token function

Code (dummy function is needed to get the error):

const functions = require('firebase-functions');
const admin = require('firebase-admin');
admin.initializeApp(functions.config().firebase);
const db = admin.firestore()

exports.some = functions.firestore.document('x/{x}').onCreate(event => {

})

function deleteCollectionAndReturnDeletedDocs(db, collectionRef, batchSize) {
    return deleteQueryBatch(db, collectionRef.limit(batchSize), batchSize, []);
  }
  async function deleteQueryBatch(db, query, batchSize, results) {
    const snapshot = await query.get();
    if (snapshot.size > 0) {
      let batch = db.batch();
      snapshot.docs.forEach(doc => {
        if (doc.exists) {
          results.push(doc.data())

        };
        batch.delete(doc.ref);
      });
      await batch.commit();
    }
    if (snapshot.size >= batchSize) {
      return deleteQueryBatch(db, query, batchSize, results);
    } else {

      return results;
    }
  }

How can I deploy the async function? I do not get this error on a server on Evennode.

3 Answers 3

4

Cloud Functions currently runs Node 6 LTS, which means it doesn't support async/await natively. If you want to use some form of async/await, you'll have to transpile your ES7 or TypeScript into ES6 so it can run in the Node container provided by Cloud Functions. The transpiler should convert async/await into equivalent code using promises.

The team is looking into also providing Node 8 LTS.

Sign up to request clarification or add additional context in comments.

Comments

0

As NodeJS 8.x is LTS now, it should work, right?

When I type "node --version" into my Google Cloud console, it shows me 8.5.0 as version of node.

But when I deploy a script via "gcloud beta functions deploy", I get a "SyntaxError: Unexpected identifier" at the position of my script where the first "await" is used.

So is async/await disabled in the Google Cloud?

2 Comments

No you see your own local node version, not googl’d version. See Doug’s link, it is still 6.x.
Ahm... when I'm on cloud.google.com and THERE (on the website!) enter the cloud console, it's technical impossible that this accesses my local console / node installation and gives me my local node version. Also, my local node version is 8.9.1 and not 8.5.0 as printed out in the cloud console. So version 8.5.0 comes definitely from the google cloud.
0

I got this error when deploying my async functions through the Google Cloud Platform. Even though I was running a newer NodeJS version locally I deployed as v6. You can deploy your function as NodeJS v6 or v8. As of January 2019, v6 was the default selection and requires selecting v8(Beta) before deployment.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.