部署Firebase函数时出错:每个then()应该返回一个值或抛出promise / always-return

时间:2019-12-28 08:49:54

标签: javascript firebase google-cloud-firestore eslint

我尝试了类似文章中推荐的常见解决方案,例如返回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);
                });
        }
});

1 个答案:

答案 0 :(得分: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}')