无法将异步功能部署到Google Cloud Functions [英] Can not deploy async function to Google Cloud Functions
问题描述
我正在使用最新版本的Node v8.9.1,但是在部署以下代码时出现此错误:
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;
}
}
如何部署异步功能?我在Evennode的服务器上没有收到此错误.
How can I deploy the async function? I do not get this error on a server on Evennode.
推荐答案
云函数当前运行的是Node 6 LTS ,这意味着它本身不支持异步/等待.如果要使用某种形式的异步/等待,则必须将ES7或TypeScript转换为ES6,以便它可以在Cloud Functions提供的Node容器中运行.转译器应使用Promise将async/await转换为等效代码.
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.
团队正在研究还提供Node 8 LTS.
The team is looking into also providing Node 8 LTS.
这篇关于无法将异步功能部署到Google Cloud Functions的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!