0

我正在使用此处记录的 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
4

1 回答 1

1

如果要使用 调用async函数await,还必须声明封闭函数async

exports.updateUser = functions.firestore
    .document('users/{userId}')
    .onUpdate(async (change, context) => {
       await doSomething(change.after.data());
});

注意我放在async第三行的位置。此语法不是 Cloud Functions 独有的 - 这是标准 JavaScript。

于 2020-09-27T22:49:23.950 回答