错误 Firebase 函数 [必须妥善处理承诺] 部署



我上周写了一个代码,它在Firebase服务器上部署时没有任何错误。 但是现在我无法在另一个帐户上再次部署它,以便 我不更改我的代码!

我的一个朋友在关于Firebase的新更新中告诉我这一点,但我找不到任何解决方案!

它显示了这些错误

Promises must be handled appropriately

block is empty
第一个错误指向

我的第一行,第二个错误指向结束"catch"块:

import * as functions from 'firebase-functions';
import * as admin from 'firebase-admin';
admin.initializeApp();
// export const helloWorld = functions.https.onRequest((request, response) => {
//  console.log("sadegh");
//  response.send("Hello from Firebase1!");
// });
//
export const sendChatNotification = functions
.firestore.document('rooms/{roomId}/messages/{messageId}')
.onCreate((snap, context) => {

    const roomId = context.params.roomId;
    const messageId = context.params.messageId;
    const newValue = snap.data();
    const receiverId = newValue.receiverId;
    const text = newValue.text;
    const type = newValue.type;
    const senderName = newValue.senderName;

    var p = admin.firestore().collection("users").doc(receiverId).get();
    p.then(snapshot2 => {
        const data2 = snapshot2.data();
        const firebaseNotificationToken = data2.firebaseNotificationToken;
        // const name = data2.name;
        if (type == 'voiceCall' || type == 'videoCall' || type == 'hangUp') {

            const channelId = newValue.channelId;
            const senderId = newValue.senderId;
            const status = newValue.status;
            console.log("type: " + type + " /status: " + status)
            let message = {
                data: {
                    type: type,
                    senderId: senderId,
                    senderName: senderName,
                    receiverId: receiverId,
                    status: status,
                    channelId: channelId,
                    roomId: roomId
                },
                token: firebaseNotificationToken
            };
            sendMessage(message)

            if (status == "canceled") {
                let message1 = {
                    notification: {
                        title: '☎ Missed voice call ',
                        body: senderName
                    },
                    token: firebaseNotificationToken
                };
                sendMessage(message1)
            } else if ((type == 'voiceCall' || type == 'videoCall') && status = '') {
                let message1 = {
                    notification: {
                        title: '☎ ' + senderName + ' is calling you',
                        body: 'tap to answer...'
                    },
                    token: firebaseNotificationToken
                };
                sendMessage(message1)
            }

        } else {
            let message = {
                notification: {
                    title: '📃 ' + senderName,
                    body: text
                },
                token: firebaseNotificationToken
            };

            sendMessage(message)
        }

        return "";
    }).catch((e) => {
        console.log('error: ' + e);
        return null;
    });

    //       return "";
    // }).catch(e=>{console.log('error: '+e)});

    return "sadegh";
});
function sendMessage(message) {
    admin.messaging().send(message)
        .then((response) => {
            // Response is a message ID string.
            console.log('Successfully sent message:', response);
        })
        .catch((error) => {
            console.log('Error sending message:', error);
        });
}

你的代码有点混乱,如果不花很长时间,就不容易理解它。

但是,下面是一段应该可以工作的代码,它涵盖了业务逻辑的一个案例。请注意异步任务返回的承诺是如何返回的。

  export const sendChatNotification = functions.firestore
      .document('rooms/{roomId}/messages/{messageId}')
      .onCreate((snap, context) => {
        const roomId = context.params.roomId;
        const messageId = context.params.messageId;
        const newValue = snap.data();
        const receiverId = newValue.receiverId;
        const text = newValue.text;
        const type = newValue.type;
        const senderName = newValue.senderName;
        var p = admin
          .firestore()
          .collection('users')
          .doc(receiverId)
          .get();
        return p.then(snapshot2 => {  // <- HERE, the promise is returned
          const data2 = snapshot2.data();
          const firebaseNotificationToken = data2.firebaseNotificationToken;
          if (type == 'voiceCall' || type == 'videoCall' || type == 'hangUp') {
            const channelId = newValue.channelId;
            const senderId = newValue.senderId;
            const status = newValue.status;
            console.log('type: ' + type + ' /status: ' + status);
            let message = {
              data: {
                type: type,
                senderId: senderId,
                senderName: senderName,
                receiverId: receiverId,
                status: status,
                channelId: channelId,
                roomId: roomId
              },
              token: firebaseNotificationToken
            };
            return admin.messaging().send(message);  // <- HERE, the promise is returned
          }
        });
  }); 

我建议您观看Firebase视频系列中有关"JavaScript Promises"的3个视频: https://firebase.google.com/docs/functions/video-series/

问题是您在 catch 块中注释了返回由于您的Firebase .get()函数必须返回一个承诺,因此在您的代码中,如果失败,它将不会返回承诺,并且会挂在那里。

使用return null或返回要由调用应用处理的内容

相关内容

  • 没有找到相关文章

最新更新