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

时间:2019-01-19 06:45:31

标签: javascript firebase google-cloud-firestore google-cloud-functions

我上周写了一个代码,它可以正确部署在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);
        });
}

2 个答案:

答案 0 :(得分:1)

您的代码有点混乱,不花大量时间就很难理解。

但是,这里下面是一段应该起作用的代码,该代码涵盖了业务逻辑的一种情况。请注意如何返回异步任务返回的承诺。

  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承诺”的3个视频:https://firebase.google.com/docs/functions/video-series/

答案 1 :(得分:0)

问题是您在catch块中评论了退货 由于您的Firebase .get()函数必须返回一个Promise,因此在您的代码中,如果失败,它将不会返回Promise并将其挂起。

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