then()返回一个值,仍有返回错误

时间:2018-02-01 15:04:23

标签: javascript node.js firebase-cloud-messaging google-cloud-functions

我遇到了一个小问题,其中errorLog正在说

21:10  warning  Avoid nesting promises                      promise/no-nesting
36:12  warning  Avoid nesting promises                      promise/no-nesting
36:66  error    Each then() should return a value or throw  promise/always-return

它所讨论的错误放在底部

 return admin.messaging().sendToDevice(token_id,payload).then(theResult =>{
                    console.log("then is returned");
                });

即使信息非常简单,调试也不会有太大的问题(就像新手一样),我也无法解决这个问题,我甚至尝试删除这个功能并且仍然没有成功部署。我找了一些类似的问题,但只是一个失败。我将非常感谢您的帮助,谢谢。

exports.sendNotification = 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;
    //console.log("User ID:" + user_id + "Notification ID : "+ 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 from_message = queryResult.data().message;

        const from_data = admin.firestore().collection("Users").doc(from_user_id).get();
        const to_data = admin.firestore().collection("Users").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 : "Nowa wiadomość od:" + from_name,
                    body : from_message,
                    icon : "default"
                }
            };

                return admin.messaging().sendToDevice(token_id,payload).then(theResult =>{
                    console.log("then is returned");
                });
        });


    });

});

1 个答案:

答案 0 :(得分:1)

您可以更新.then()块,如:

.then(theResult => console.log("then is returned"));
  • always-return:需要在每个then()内返回,以便创建可读且可重复使用的Promise链。