简体   繁体   English

异步 Firebase 云 Function 触发器 - 在 catch 块上返回什么?

[英]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.

相关问题 如何从 firebase 云 function 返回数据并返回异步响应? - How to return data from firebase cloud function and return async response? 在 linkWithCredential() 上触发 Firebase 云函数 - Trigger Firebase Cloud Function on linkWithCredential() 在特定日期和时间触发云 function firebase - Trigger cloud function on specific date and time firebase 如何测量云 Function 中 Firebase 中特定代码块的执行时间? - How to measure execution time of a particular block of code in Cloud Function for Firebase? 如何从 Firebase 云调用异步函数 Function - How to call async functions from a Firebase Cloud Function Firebase 的云功能是否准时触发? - Cloud Functions for Firebase trigger on time? 从 Firebase 可调用 function(云功能)返回 stream 的正确方法? - Proper way to return stream from Firebase callable function (Cloud function)? Firebase 云函数捕获/处理错误 - Firebase cloud functions catch/handle error 无法计算 Firebase 云触发器 function 中的新旧值差异 - Can't calculate the old and new values difference in Firebase cloud trigger function Firebase 云function嵌套条件下如何满足返回promise? - How to satisfy requirement to return promise in Firebase cloud function with nested conditions?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM