【问题标题】:then() returns a value, still there is a return errorthen() 返回一个值,还是有返回错误
【发布时间】:2018-02-01 15:04:23
【问题描述】:

我遇到了一个小问题,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");
                });
        });


    });

});

【问题讨论】:

  • 尝试更新然后喜欢.then(theResult => console.log("then is returned"));
  • 哇,我从没想过这会是答案。如果您想随意做出官方回答,我可以将其标记为最佳。非常感谢
  • 当您没有返回任何内容 (undefined) 时,为什么还要使用 return admin.firestore()...return Promise.all()return admin.messaging()...? O.o
  • @Andreas 我无法真正回答您的问题,我是新手(使用 node.js 的第一天)并且我正在学习教程:D

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


【解决方案1】:

您可以像这样更新.then() 块:

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

【讨论】:

    猜你喜欢
    • 2020-07-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-03-10
    • 2011-11-10
    • 1970-01-01
    • 2017-06-21
    • 1970-01-01
    相关资源
    最近更新 更多