简体   繁体   English

Firebase Cloud Messaging生成错误

[英]Firebase Cloud Messaging generating error

I am trying to trigger a push notification whenever a document field is written to. 每当尝试将文档字段写入时,我都试图触发推送通知。 Since I am brand new to node.js I am having difficulty debugging this seemly easy function. 由于我是node.js的新手,因此调试该看似简单的函数很困难。 Here is my code: 这是我的代码:

// The Cloud Functions for Firebase SDK to create Cloud Functions and setup triggers.
const functions = require('firebase-functions');

// The Firebase Admin SDK to access the Firebase Realtime Database.
const admin = require('firebase-admin');
admin.initializeApp(functions.config().firebase);

exports.sendNotification = functions.firestore.document("Customer_Data/{userEmail}/Placed_Orders/{orderId}/status").onWrite(event => {
  const userEmail = event.params.userEmail;

  const message = "Your meal will be delivered in 2 hours";
  const title = "Eat Away";

  const toUser = admin.firestore().collection("Customer_Data").doc(userEmail).get();

  return admin.firestore().collection("Customer_Data").doc({userEmail}).get().then(queryResult => {
    const tokenId = queryResult.data().tokenId;

    const notificationContent = {
      notification: {
        title: title,
        body: message,
      }
    }
  });

  return admin.messaging().sendToDevice(tokenId , notificationContent).then(function(response) {
    console.log("Message sent successfully");
  }).catch(function(error){
    console.log("Error sending message:", error);
  });

});

And here is the error I receive in the terminal: 这是我在终端中收到的错误:

 53:11  warning  Avoid nesting promises                      promise/no-nesting
  53:11  warning  Avoid nesting promises                      promise/no-nesting
  53:77  warning  Unexpected function expression              prefer-arrow-callback
  53:77  error    Each then() should return a value or throw  promise/always-return
  55:13  warning  Unexpected function expression              prefer-arrow-callback

✖ 5 problems (1 error, 4 warnings)
  0 errors and 2 warnings potentially fixable with the `--fix` option.

npm ERR! code ELIFECYCLE
npm ERR! errno 1
npm ERR! functions@ lint: `eslint .`
npm ERR! Exit status 1
npm ERR! 
npm ERR! Failed at the functions@ lint script.
npm ERR! This is probably not a problem with npm. There is likely additional logging output above.

Error: functions predeploy error: Command terminated with non-zero exit code1

I did not use a promise because I didn't see the point of it. 我没有使用诺言,因为我没有看到诺言的意义。 But at the same time I tried it out and it did not help. 但是同时我尝试了一下,但没有帮助。

Not seeing the point of promises is typically a reason to learn more about them, not to not use them. 没有看到诺言的要点通常是更多地了解诺言而不是不兑现诺言的原因。

The way you've now written your code means that a part of it never runs. 您现在编写代码的方式意味着它的一部分永远不会运行。 This doesn't have anything to do with promises yet, but simply with the fact you have two return statements in the main flow of your code: 这与Promise无关,而仅仅是因为您在代码的主要流程中有两个return语句:

exports.sendNotification = functions.firestore.document("Customer_Data/{userEmail}/Placed_Orders/{orderId}/status").onWrite(event => {
  ...
  return admin.firestore().collection("Customer_Data").doc({userEmail}).get().then(queryResult => {
    ...
  });

  // Nothing below this line ever executes
  return admin.messaging().sendToDevice(tokenId , notificationContent).then(function(response) {
    console.log("Message sent successfully");
  }).catch(function(error){
    console.log("Error sending message:", error);
  });
});

In this case you need the results from Firestore to send the FCM message, which means you need to wait for the Firestore promise to resolve (with then() or await ) before calling FCM: 在这种情况下,您需要来自Firestore的结果来发送FCM消息,这意味着您需要在调用FCM之前await Firestore承诺解决(使用then()await ):

exports.sendNotification = functions.firestore.document("Customer_Data/{userEmail}/Placed_Orders/{orderId}/status").onWrite(event => {
  const userEmail = event.params.userEmail;

  const message = "Your meal will be delivered in 2 hours";
  const title = "Eat Away";

  const toUser = admin.firestore().collection("Customer_Data").doc(userEmail).get();

  return admin.firestore().collection("Customer_Data").doc({userEmail}).get().then(queryResult => {
    const tokenId = queryResult.data().tokenId;

    notificationContent = {
      notification: {
        title: title,
        body: message,
      }
    }

    return admin.messaging().sendToDevice(tokenId , notificationContent);
  })
});

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

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