简体   繁体   English

部署时出错firebase函数[必须正确处理承诺]

[英]error firebase functions [Promises must be handled appropriately] on deploy

I was written a code previous week and it deploys without any error on firebase server. 我上周写了代码,它在firebase服务器上部署时没有任何错误。 but now I cannot deploy it again on another account in orders to I don't change my code! 但现在我无法按顺序将其再次部署到另一个帐户,因为我不更改代码!

one of my friends tell me this in about new update of firebase but I don't find any solution for this! 我的一个朋友告诉我有关Firebase的新更新的内容,但是我没有找到任何解决方案!

it shows these errors 它显示了这些错误

Promises must be handled appropriately

and

block is empty

the first error pointed to my first line and the second one pointed to end 'catch' block : 第一个错误指向我的第一行,第二个错误指​​向结束“ 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);
        });
}

Your code is a bit messy and it is not really easy to understand it without dedicating a long time. 您的代码有点混乱,不花很长时间就很难理解。

However, here is below a piece of code that should work and that cover one case of your Business Logic. 但是,这里下面是一段应该起作用的代码,它涵盖了您的业务逻辑的一种情况。 Note how the promises returned by the asynchronous tasks are returned. 注意如何返回异步任务返回的承诺。

  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
          }
        });

  }); 

I would suggest you watch the 3 videos about "JavaScript Promises" from the Firebase video series: https://firebase.google.com/docs/functions/video-series/ 我建议您观看Firebase视频系列中有关“ JavaScript承诺”的3个视频: https ://firebase.google.com/docs/functions/video-series/

The problem is you commented the return in your catch block As your Firebase .get() function must return a promise, in your code, if it fails, it won't return a promise and it will hang there. 问题是您注释了catch块中的返回,因为Firebase .get()函数必须返回一个诺言,在您的代码中,如果失败,它将不会返回一个诺言并将其挂在那里。

either use return null or return something to be handled by the calling app 使用return null或返回要由调用应用程序处理的内容

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM