如何在 Cloud Firestore 触发器中运行异步代码

我正在使用此处记录的 Cloud Firestore 触发器。 https://firebase.google.com/docs/functions/firestore-events


如何从这些函数之一运行异步代码?这是我想做的一个例子。


async function doSomething(data) {

   // ...

}


exports.updateUser = functions.firestore

    .document('users/{userId}')

    .onUpdate((change, context) => {

       await doSomething(change.after.data());

});

当我尝试部署此功能时,我收到此错误消息。


return await doSomething(change.after.data());

       ^^^^^


SyntaxError: await is only valid in async function


慕桂英546537
浏览 92回答 1
1回答

慕码人8056858

如果要async使用 调用函数await,则还必须声明封闭函数async。exports.updateUser = functions.firestore    .document('users/{userId}')    .onUpdate(async (change, context) => {       await doSomething(change.after.data());});注意我放在async第三行的位置。这种语法不是 Cloud Functions 独有的——这是标准的 JavaScript。
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript