【问题标题】:On Deploying the Firebase Cloud Function getting an error of "Each then should return a value or throw"在部署 Firebase 云函数时收到“每个都应该返回一个值或抛出”错误
【发布时间】:2020-01-22 17:15:54
【问题描述】:

我是 Cloud Functions 的新手,所以我在使用以下代码时遇到了问题,错误出现在最后提到 console.log 的部分,请协助我应该如何成功部署函数,因为我正在关注教程名称没有这样的错误。

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

        });

    });




});

【问题讨论】:

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


    【解决方案1】:

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

    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 常量。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-02-19
      • 2018-09-25
      • 1970-01-01
      • 2018-09-11
      • 2020-04-02
      相关资源
      最近更新 更多