[英]Async Firebase Cloud Function trigger - What to return on catch block?
I have written the trigger below and I'm not sure what I should return in case a catch
block is called.我已经在下面编写了触发器,但我不确定在调用
catch
块时应该返回什么。 I know that Firebase docs say that triggers should always return a Promise
...我知道 Firebase 文档说触发器应该总是返回
Promise
...
exports.sendPushNotificationForNewMessage = functions.firestore.document("messages/{messageId}").onCreate(async (snap, context) => {
const message = snap.data()
const chatRoomId = message.chatRoomId
const senderId = message.user.id
const senderUsername = message.user.username
try {
const chatRoom = await admin.firestore().collection("chatRooms").doc(chatRoomId).get()
const receiverId = chatRoom.data().userIds.find(userId => userId != senderId)
const receiver = await admin.firestore().collection("users").doc(receiverId).get()
const deviceToken = receiver.data().deviceToken
if (deviceToken) {
const payload = {
notification: {
title: "popster",
body: `New DM from ${senderUsername} 💬`,
badge: "1",
sound: "pop.m4a"
},
data: {
}
}
console.log(payload);
return admin.messaging().sendToDevice(deviceToken, payload)
} else {
return null
}
} catch (error) {
return null
}
})
The async
function wraps your response in a Promise
so here your return type is Promise<MessagingDevicesResponse | null>
async
function 将您的响应包装在Promise
所以这里您的返回类型是Promise<MessagingDevicesResponse | null>
Promise<MessagingDevicesResponse | null>
and that will terminate the Cloud Function . Promise<MessagingDevicesResponse | null>
这将终止 Cloud Function 。
I'm not sure what I should return in case a catch block is called.
如果调用 catch 块,我不确定应该返回什么。
Background functions do not return any value/error to client so you can just return null;
后台函数不会向客户端返回任何值/错误,因此您只需
return null;
. .
Also checkout this Firecast for more information.另请查看此 Firecast以获取更多信息。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.