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



>我遇到了一个小问题,错误日志说

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()块,如下所示:

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

相关内容

  • 没有找到相关文章

最新更新