简体   繁体   English

尝试在Firebase FireStore中创建函数时Javascript Promises错误

[英]Error in Javascript Promises while trying to create a function in Firebase FireStore

so I've been following this 5 part tutorial series as to how notifications can be sent from device to device using Firebase Cloud Storage and the javascript I wrote seems to be incorrect because I keep getting the error that "Each then() should return a value or throw promise/always-return". 因此,我一直遵循这个由5部分组成的教程系列,内容涉及如何使用Firebase Cloud Storage在设备之间发送通知,而我编写的JavaScript似乎是不正确的,因为我不断收到“每个then()应该返回一个价值或承诺/总是回报”。 Can someone tell me as to how I should correct this? 有人可以告诉我该如何纠正吗?

index.js index.js

'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");
      });
    });
  });
});

Apart from the above error I also get a "parsing" error as follows 除了上述错误外,我还收到如下“解析”错误

32:18 error Parsing error: Unexpected token default

"Each then() should return a value or throw promise/always-return" “每个then()应该返回一个值或抛出promise / always-return”

This indicates that one of your .then 's doesn't return a value (or throw an error) 这表明您的.then之一未返回值(或引发错误)

Here is the culprit: 罪魁祸首:

.then(result => {
    console.log("Notification Sent");
});

So add a return . 因此,添加return Now, since console.log return value is undefined and in a function having no return statement there's an implied return undefined 现在,由于console.log返回值是undefined并且在没有return语句的函数中,存在隐含的return undefined

Therefore the code below results in exactly the same behaviour (ie return undefined ), and will prevent that warning message 因此,下面的代码将导致完全相同的行为(即, return undefined ),并将阻止该警告消息

.then(result => {
    return console.log("Notification Sent");
});

As far as your error regarding default , that's simply because default is a reserved word in javascript (and it's not even declared in your code anyway) - it's like trying to use if or while as a variable name 至于关于default的错误,那仅仅是因为default是javascript中的保留字(无论如何它都没有在代码中声明)-就像试图将ifwhile用作变量名一样


Another potential issue I see in your code is 我在您的代码中看到的另一个潜在问题是

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();

Should that be .doc(from_user_id) - otherwise, what is the point of var from_user_id = queryResult.data().from; 应该是.doc(from_user_id) -否则, var from_user_id = queryResult.data().from;


And finally, as an aside, I see that you are nesting your promises rather than chaining them. 最后,顺便说一句,我看到您正在嵌套您的诺言,而不是将它们链接在一起。 One benefit of promises is you can avoid the "callback pyramid of hell/doom" Promise的好处之一是您可以避免“地狱/末日的回调金字塔”

Your code could be written like below instead which avoids the "pyramid" - there's also some other ES6/ES7+ tricks in there so that queryResult.data() and to_data.data() only needs to be called once 您的代码可以像下面这样写而不是避免“金字塔”-那里还有其他一些ES6 / ES7 +技巧,因此queryResult.data()to_data.data()只需调用一次

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");
    });
});

You are using the reserved keyword default which you cannot use. 您使用的保留关键字default无法使用。 I guess you meant to represent this as a string, eg "default" 我想您打算将其表示为字符串,例如"default"

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM