Я пытаюсь сделать функцию push-уведомлений всякий раз, когда обновление базы данных отправляет уведомление, но функция сначала получает ошибку
Ошибка:
TypeError: Cannot read property 'val 'из неопределенного в exports.fcmSend.functions.database.ref.onCreate.event (/srv/index.js:9:31) в cloudFunction (/srv/node_modules/firebase-functions/lib/cloud-functions.js:131: 23) в /worker/worker.js:825:24 в at process._tickDomainCallback (внутренняя / process / next_tick.js: 229: 7)
моя функция
const functions = require('firebase-functions');
const admin = require("firebase-admin");
admin.initializeApp();
exports.fcmSend = functions.database.ref('/messages/{userId}/{messageId}').onCreate(event => {
console.log('event', event)
const message = event.after.val()
const userId = event.params.userId
const payload = {
notification: {
title: message.title,
body: message.body,
icon: "https://placeimg.com/250/250/people"
}
};
admin.database()
.ref(`/fcmTokens/${userId}`)
.once('value')
.then(token => token.val() )
.then(userFcmToken => {
return admin.messaging().sendToDevice(userFcmToken, payload)
})
.then(res => {
console.log("Sent Successfully", res);
})
.catch(err => {
console.log('err', err);
});
});