首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >尝试在Firebase FireStore中创建函数时,Javascript Promises中出现错误

尝试在Firebase FireStore中创建函数时,Javascript Promises中出现错误
EN

Stack Overflow用户
提问于 2018-09-27 08:53:23
回答 2查看 141关注 0票数 0

所以我一直在遵循这个5部分教程系列,关于如何使用Firebase云存储从一个设备发送通知到另一个设备,我编写的javascript似乎不正确,因为我一直收到错误"Each then()应该返回值或抛出promise/always- return“。谁能告诉我该如何纠正这个错误?

index.js

代码语言:javascript
复制
'use-strict'

const functions = require('firebase-functions');
const admin = require('firebase-admin');
admin.initializeApp(functions.config().firebase);

exports.sendNotification = functions.firestore.document("Users/{user_id}/Notifications/{notification_id}").onWrite((event) => {

  var user_id = event.params.user_id;
  var 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 => {
    var from_user_id = queryResult.data().from;
    var from_message = queryResult.data().message;

    var from_data = admin.firestore().collection("").doc("from_user_id").get();
    var to_data = admin.firestore().collection("Users").doc(user_id).get();

    return Promise.all([from_data, to_data]).then(result => {
      var from_name = result[0].data().name;
      var to_name = result[1].data().name;
      var token_id = result[1].data().token_id;

      console.log("From: " + from_name + " | To : " + to_name);

      var payload = {
        notification : {
          title : "Notification From : " + from_name,
          body : from_message,
          icon : default
        }
      };

      return admin.messaging().sendToDevice(token_id, payload).then(result => {
        console.log("Notification Sent");
      });
    });
  });
});

除了上面的错误之外,我还得到了一个"parsing“错误,如下所示

代码语言:javascript
复制
32:18 error Parsing error: Unexpected token default
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2018-09-27 09:11:49

“每个then()都应该返回值或抛出promise/always- return”

这表明您的某个.then没有返回值(或抛出错误)

这就是罪魁祸首:

代码语言:javascript
复制
.then(result => {
    console.log("Notification Sent");
});

所以添加一个return。现在,由于console.log返回值为undefined,并且在没有返回语句的函数中有一个隐含的return undefined

因此,下面的代码导致完全相同的行为(即return undefined),并将阻止该警告消息

代码语言:javascript
复制
.then(result => {
    return console.log("Notification Sent");
});

至于您关于default的错误,这很简单,因为default是javascript中的保留字(而且您的代码中也没有声明它)-这就像试图使用ifwhile作为变量名

我在您的代码中看到的另一个潜在问题是

代码语言:javascript
复制
var from_user_id = queryResult.data().from;
var from_message = queryResult.data().message;

//***                                                vvvvvvvvvvvvvv
var from_data = admin.firestore().collection("").doc("from_user_id").get();
var to_data = admin.firestore().collection("Users").doc(user_id).get();

这应该是.doc(from_user_id) -否则,var from_user_id = queryResult.data().from;的意义何在

最后,顺便说一句,我看到你是在嵌套你的承诺,而不是把它们捆绑在一起。承诺的一个好处是你可以避免“地狱/末日的回调金字塔”

您的代码可以像下面这样编写,这样就避免了“金字塔”--还有一些其他的ES6/ES7+技巧,这样queryResult.data()to_data.data()只需要被调用一次

代码语言:javascript
复制
const functions = require('firebase-functions');
const admin = require('firebase-admin');
admin.initializeApp(functions.config().firebase);

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;

    return admin.firestore().collection("Users").doc(user_id).collection("Notifications").doc(notification_id).get()
    .then(queryResult => {
        const {from:from_user_id, message:from_message} = queryResult.data();

        const from_data = admin.firestore().collection("").doc(from_user_id).get();
        const to_data = admin.firestore().collection("Users").doc(user_id).get();

        return Promise.all([from_data, to_data, from_message]); // added from_message so it's available in the next .then
    })
    .then(([from_data, to_data, from_message]) => { // added from_message
        const from_name = from_data.data().name;
        const {name:to_name, token_id} = to_data.data();

        console.log("From: " + from_name + " | To : " + to_name);

        const payload = {
            notification : {
                title : "Notification From : " + from_name,
                body : from_message,
                icon : 'default'
            }
        };

        return admin.messaging().sendToDevice(token_id, payload);
    })
    .then(result => {
        return console.log("Notification Sent");
    });
});
票数 1
EN

Stack Overflow用户

发布于 2018-09-27 09:03:03

您正在使用不能使用的保留关键字default。我猜您的意思是将其表示为字符串,例如"default"

票数 -1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/52527914

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档