Firebase函数出现错误-“ TypeError:无法读取未定义的属性'from'...”

时间:2019-05-20 15:06:48

标签: firebase google-cloud-firestore google-cloud-functions

我正在开发android应用程序,并使用firebase函数通过firebase云消息传递发送通知。我对firebase函数了解不多。我尝试了一些教程。

我在Firebase功能日志中遇到如下错误:

图片链接... error image

TypeError:无法读取未定义的属性“ from”     在admin.firestore.collection.doc.collection.doc.get.then.queryResult(/user_code/index.js:14:42)     在process._tickDomainCallback(internal / process / next_tick.js:135:7)

如果您知道解决方案,请编辑我的代码并粘贴作为答案。预先感谢。

我的索引代码如下

      'use-strict'
const functions = require('firebase-functions');
const admin = require('firebase-admin');
admin.initializeApp();
exports.sendNotification = functions.firestore.document("Users/{user_id}/Notifications/{notification_id}").onWrite((change,context)=> {

 const user_id = context.params.user_id;
 const notification_id = context.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 type = queryResult.data().type;

  console.log("FROM_USER ID : "+from_user_id+" TYPE "+type);

  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: {
     tag : from_user_id+"Follow",
     title : "Request From : "+from_name,
     icon  : "follow_icon_for_notification",
     color : "white",
     sound : "TYPE_NOTIFICATION",
     body  : "Click here to accept Follow Request.",
     click_action:"jony.Activities.ONFOLLOWREQUESTRECEIVED"
    },
    data : {
     message : "Click here to accept Follow Request.",
     user_id : from_user_id
    }
   };

   return admin.messaging().sendToDevice(token_id, payload).then(result => {

    var db = admin.firestore();

    const FieldValue = require('firebase-admin').firestore.FieldValue;

    var notificationRef = db.collection("Users").doc(user_id).collection("Notifications").doc(notification_id).delete();

    return console.log("Follow notification sent");

   });

  });

 });

}); 

1 个答案:

答案 0 :(得分:0)

您会使用change.after.data()吗?

请参见https://firebase.google.com/docs/functions/firestore-events

"use-strict";
const functions = require("firebase-functions");
const admin = require("firebase-admin");
admin.initializeApp();
exports.sendNotification = functions.firestore
  .document("Users/{user_id}/Notifications/{notification_id}")
  .onWrite((change, context) => {
    const user_id = context.params.user_id;
    const notification_id = context.params.notification_id;

    console.log("USER ID : " + user_id + " NOTIFICATION ID " + notification_id);

    console.log("change.after.exists:" + change.after.exists);
    console.log("change.after.data():" + change.after.data());

    const data = change.after.data();

    const from_user_id = data.from;
    const type = data.type;

    console.log("FROM_USER ID : " + from_user_id + " TYPE " + type);

    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: {
          tag: from_user_id + "Follow",
          title: "Request From : " + from_name,
          icon: "follow_icon_for_notification",
          color: "white",
          sound: "TYPE_NOTIFICATION",
          body: "Click here to accept Follow Request.",
          click_action: "jony.Activities.ONFOLLOWREQUESTRECEIVED"
        },
        data: {
          message: "Click here to accept Follow Request.",
          user_id: from_user_id
        }
      };

      return admin
        .messaging()
        .sendToDevice(token_id, payload)
        .then(result => {
          var db = admin.firestore();

          const FieldValue = require("firebase-admin").firestore.FieldValue;

          var notificationRef = db
            .collection("Users")
            .doc(user_id)
            .collection("Notifications")
            .doc(notification_id)
            .delete();

          return console.log("Follow notification sent");
        });
    });
  });