在部署Firebase Cloud Function时,出现错误“每个然后应返回值或抛出”

时间:2019-09-22 11:35:36

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

我是Cloud Functions的新手,所以我对下面的代码有疑问,错误是在提到console.log的最后一部分,请协助我完成成功部署Function的工作。按照教程进行操作,名称没有这种错误。

'use-strict'

const functions = require('firebase-functions');
const admin = require("firebase-admin");

admin.initializeApp(functions.config().firebase);
exports.sendNotifications = functions.firestore.document("users/{user_id}/notifications/{notification_id}").onWrite(event => {


    const user_id = event.params.user_id;
    const notification_id = event.params.notification_id;




    return admin.firestore().collection("users").doc(user_id).collection("notifications").doc(notification_id).get().then(queryResult => {

        const from_user_id = queryResult.data().from;
        const message =  queryResult.data().message;

        const from_data = admin.firestore().collection("users").doc(from_user_id).get();
        const to_data = admin.firestore().collection("user").doc(user_id).get();

        return Promise.all([from_data, to_data]).then(result => {
            const from_name = result[0].data().name;
            const to_name = result[1].data().name;
            const token_id  = result[1].data().token_id;

            const payload  = {
                notification: {
                    title: "Notification from:" + from_name,
                    body: message,
                    icon: "default"


                }
            };

            return admin.messaging().sendToDevice(token_id, payload).then(result =>{

                console.log("notifcation sent");
            });

        });

    });




});

1 个答案:

答案 0 :(得分:1)

通过链接您的Promises并在最后一个null中返回then(),如下所示,您应该解决问题:

exports.sendNotifications = functions.firestore.document("users/{user_id}/notifications/{notification_id}").onWrite(event => {

    const user_id = event.params.user_id;
    const notification_id = event.params.notification_id;

    return admin.firestore().collection("users").doc(user_id).collection("notifications").doc(notification_id).get()
        .then(queryResult => {

            const from_user_id = queryResult.data().from;
            const message = queryResult.data().message;

            const from_data = admin.firestore().collection("users").doc(from_user_id).get();
            const to_data = admin.firestore().collection("user").doc(user_id).get();

            return Promise.all([from_data, to_data]);
        })
        .then(result => {
            const from_name = result[0].data().name;
            const to_name = result[1].data().name;
            const token_id = result[1].data().token_id;

            const payload = {
                notification: {
                    title: "Notification from:" + from_name,
                    body: message,
                    icon: "default"

                }
            };

            return admin.messaging().sendToDevice(token_id, payload)

        })
        .then(messagingResponse => {
            console.log("notification sent");
            return null;   //Note the return null here, watch the 3 videos about "JavaScript Promises" from the Firebase video series: https://firebase.google.com/docs/functions/video-series/
        });

});

您可以查看相应的MDN文档:https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Using_promises#Chaining

另外,请注意,在您的代码中,您似乎没有使用to_name常量。