Reputation: 13103
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.
Upvotes: 4
Views: 2058
Reputation: 2680
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.
Upvotes: 0
Reputation: 89
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?
Upvotes: 0
Reputation: 317828
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.
Upvotes: 4