简体   繁体   English

在部署 Firebase 云 Function 时收到“每个都应该返回值或抛出”错误

[英]On Deploying the Firebase Cloud Function getting an error of “Each then should return a value or throw”

I am new to Cloud Functions so I having issues with below code, the error is in the last part where the console.log is mentioned, please assist what shall I been done to deploy the Function successfully, as I am following a tutorial there is no such error for the name.我是 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");
            });

        });

    });




});

By chaining your Promises and returning null in the last then() , as follows, you should solve your problem:通过链接您的 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/
        });

});

You may have a look at the corresponding MDN documentation: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Using_promises#Chaining您可以查看相应的 MDN 文档: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Using_promises#Chaining

Also, note that, in your code, it seems that you are not using the to_name constant.另外,请注意,在您的代码中,您似乎没有使用to_name常量。

暂无
暂无

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

相关问题 部署Firebase云函数时出错每个then()应该返回一个值或抛出 - Error on deploy Firebase cloud function Each then() should return a value or throw 每个 then() 应该在 firebase 云 function 中返回一个值或抛出错误 - Each then() should return a value or throw error in firebase cloud function 得到错误每个 then() 应该返回一个值或抛出我的 firebase function - getting error Each then() should return a value or throw in my firebase function 部署 Firebase 函数时出错:每个 then() 都应该返回一个值或抛出 promise/always-return - Error deploying Firebase function: Each then() should return a value or throw promise/always-return 每个 then() 都应该返回一个值或抛出 Firebase 云函数 - Each then() should return a value or throw Firebase cloud functions Firebase云消息传递每个then()应该返回一个值或抛出Promise / Always-Return - Firebase cloud messaging Each then() should return a value or throw promise/always-return 错误每个 then() 应该返回一个值或抛出 - Error Each then() should return a value or throw 在使用node.js中的firebase从Google云端存储中读取时,ESlint发出“每次应返回一个值或引发错误”的问题 - ESlint issue “Each then should return a value or throw” while reading from google cloud store using firebase in node.js 每个 then() 应该返回一个值或抛出 - Each then() should return a value or throw Node.js 错误:每个 then() 应该返回一个值还是抛出? - Node.js error: Each then() should return a value or throw?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM