【问题标题】:Firebase Cloud Messaging generating errorFirebase 云消息生成错误
【发布时间】:2019-11-21 15:26:40
【问题描述】:

每当写入文档字段时,我都会尝试触发推送通知。由于我是 node.js 的新手,我很难调试这个看似简单的功能。这是我的代码:

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

});

这是我在终端收到的错误:

 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

我没有使用承诺,因为我没有看到它的意义。但同时我尝试了它并没有帮助。

【问题讨论】:

  • 如果您想用 JavaScript 为 Cloud Functions 编写有效的代码,您绝对需要了解 Promise 以及如何在各种情况下使用它们。
  • 同意。我确实阅读了文档,其中大部分内容都在我脑海中浮现,所以我需要找到一个带有示例的好教程,以便我可以完全掌握这个概念。

标签: javascript node.js promise google-cloud-functions


【解决方案1】:

没有看到承诺的意义通常是更多了解它们的理由,而不是不使用它们。

您现在编写代码的方式意味着它的一部分永远不会运行。这与 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);
  });
});

在这种情况下,您需要 Firestore 的结果来发送 FCM 消息,这意味着您需要等待 Firestore 承诺解决(使用 then()await),然后再调用 FCM:

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

【讨论】:

    猜你喜欢
    • 2018-11-25
    • 2016-11-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-07-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多