Ошибка типа: не удается прочитать свойство потомка неопределенного - PullRequest
0 голосов
/ 26 сентября 2018

Я пытаюсь создать функцию firebase для node.js, которая отправляет уведомление пользователю каждый раз, когда узел добавляется или обновляется внутри родительского узла «Уведомления» в моей базе данных реального времени.

Вот мойindex.js-

let functions = require('firebase-functions');

let admin = require('firebase-admin');

admin.initializeApp();

exports.sendNotification = functions.database.ref('/Notifications/{postId}').onWrite((change, context) => {

//get the userId of the person receiving the notification because we need to get their token
 const receiverId = change.after.data.child('receiver_token').val();
 console.log("receiverId: ", receiverId);



 //get the message
 const message = change.after.data.child('content').val();
 console.log("message: ", message);



  const token = receiver_token;

  console.log("Construction the notification message.");
  const payload = {
    data: {
      data_type: "direct_message",
      title: "Open Up",
      message: message,

    }
  };

  return admin.messaging().sendToDevice(token, payload);
});

Но каждый раз, когда появляется ошибка, она говорит, что результат change.after.data не определен.В чем проблема.Как мне это исправить?

Моя база данных в реальном времени:

enter image description here

1 Ответ

0 голосов
/ 26 сентября 2018

Измените это:

const receiverId = change.after.data.child('receiver_token').val();
console.log("receiverId: ", receiverId);
//get the message
const message = change.after.data.child('content').val();
console.log("message: ", message); 

на это:

const receiverId = change.after.child('receiver_token').val();
console.log("receiverId: ", receiverId);
//get the message
const message = change.after.child('content').val();
console.log("message: ", message);

И onWrite, и onUpdate имеют параметр change, который имеет поля до и после,Каждый из них является DataSnapshot с теми же методами, доступными в admin.database.DataSnapshot

admin.database.DataSnapshot, не содержит поле с именем data, поэтому выполучить неопределенную ошибку.

...