Я пытаюсь создать функцию 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](https://i.stack.imgur.com/tLmOn.jpg)