【问题标题】:Error deploying Firebase function: Each then() should return a value or throw promise/always-return部署 Firebase 函数时出错:每个 then() 都应该返回一个值或抛出 promise/always-return
【发布时间】:2019-12-28 08:49:54
【问题描述】:

我已经尝试过类似帖子中推荐的常见解决方案,例如返回 null 或重新调整非 null 值,但这只会导致另一个部署错误,如下所示:

未能配置触发器提供程序/cloud.firestore/eventTypes/document.create@firestore.googleapis.com....

我也在使用 eslint。我的功能代码如下:

exports.onCreateActivityFeedItem = functions
    .firestore
    .document('/feed/{userId}/feedItems/{activityFeedItem')
    .onCreate(async (snapshot, context) => {
        console.log('Activity Feed Item Create', snapshot.data());

        // 1) Get user connected to the feed
        const userId = context.params.userId;
        const userRef = admin.firestore().doc(`users/${userId}`);
        const doc = await userRef.get();

        // 2) Once we have user, check if the have notification token
        //send notification if they have a token
        const androidNotificationToken = doc.data().androidNotificationToken;
        const createdActivityFeedItem = snapshot.data();

        if(androidNotificationToken){
            //send the notification
            sendNotification(androidNotificationToken, createdActivityFeedItem);
        }else{
            console.log("No token for user, cannot send notification");
        }

        function sendNotification(androidNotificationToken, activityFeedItem){
            let body;

            // 3) switch body value based off of notification type
            switch (activityFeedItem.type){
                case "comment":
                 body = `${activityFeedItem.username} replied: ${activityFeedItem.commentData}`;
                 break;
                case "like":
                 body = `${activityFeedItem.username} liked your post`;
                 break;
                case "follow":
                 body = `${activityFeedItem.username} started following your pet`;
                 break;
                default:
                 break;
            }

            // 4) Create message for push notification
            const message = {
                notification: { body },
                token: androidNotificationToken,
                data: { recipient: userId }
            };

            // 5) Send message with admin.messaging()
            admin
                .messaging()
                .send(message)
                .then(response => {
                    // Response is a message ID string
                    console.log("Successfully sent message", response);
                    //return 1;   //Last edition trying to fix bug

                }).catch(error => {
                    console.log("Error sending message", error);
                });
        }
});

【问题讨论】:

  • 请将您的问题缩小到一个问题。您在这里引用了两个问题,一个与 eslint 有关,另一个与触发器的创建有关。这两个问题可能彼此无关。如果你创建一个 MCVE 会很有帮助:stackoverflow.com/help/minimal-reproducible-example

标签: javascript firebase google-cloud-firestore eslint


【解决方案1】:

改变这个:

admin
            .messaging()
            .send(message)
            .then(response => {
                // Response is a message ID string
                console.log("Successfully sent message", response);
                //return 1;   //Last edition trying to fix bug

            }).catch(error => {
                console.log("Error sending message", error);
            });
    }

进入这个:

return admin
            .messaging()
            .send(message)
            .then(response => {
                // Response is a message ID string
                console.log("Successfully sent message", response);
               // return 1;   //Last edition trying to fix bug

            }).catch(error => {
                console.log("Error sending message", error);
            });
    }

您在document 中也缺少}

改变: document('/feed/{userId}/feedItems/{activityFeedItem')

进入这个:

document('/feed/{userId}/feedItems/{activityFeedItem}')

【讨论】:

  • 感谢您的评论。上述建议导致相同的错误:每个 then() 都应该返回一个值或抛出 promise/always-return
  • 把return 1去掉注释,改成return null
  • 尝试(返回 null)会导致部署错误:(无法配置触发器提供程序/cloud.firestore...)。还尝试返回 1 个广告,它给出了相同的错误
  • document('/feed/{userId}/feedItems/{activityFeedItem')改成这个document('/feed/{userId}/feedItems/{activityFeedItem} ')
  • 这仍然给出相同的部署错误:(无法配置触发器提供程序/cloud.firestore...)
猜你喜欢
  • 2018-09-11
  • 1970-01-01
  • 1970-01-01
  • 2019-02-19
  • 2018-09-25
  • 1970-01-01
  • 2019-06-26
  • 1970-01-01
  • 2020-01-22
相关资源
最近更新 更多