Я новичок в функциях в Firebase, прочитал некоторые изменения в официальной документации, но, поскольку я делаю систему уведомлений, она выдает мне эту ошибку, когда я пытаюсь получить свой токен, я думаю.
Мой код
'use strict'
const functions = require('firebase-functions');
const admin = require('firebase-admin');
admin.initializeApp(functions.config().firebase);
/*
* 'OnWrite' works as 'addValueEventListener' for android. It will fire the function
* everytime there is some item added, removed or changed from the provided 'database.ref'
* 'sendNotification' is the name of the function, which can be changed according to
* your requirement
*/
exports.sendNotification = functions.database.ref('/notifications/{user_id}/{notification_id}').onWrite((change, context) => {
/*
* You can store values as variables from the 'database.ref'
* Just like here, I've done for 'user_id' and 'notification'
*/
const user_id = context.params.user_id;
const notification_id = context.params.notification_id;
console.log('Tenemos una notificacion para mandar a : ', context.params.user_id);
if(!context.data.val()){
return console.log('Una notificacion se elimino de la base de datos : ', notification_id);
}
const deviceToken = admin.database().ref(`/users/${user_id}/device_token`).once('value');
return deviceToken.then(result => {
const token_id = result.val();
/*
* We are creating a 'payload' to create a notification to be sent.
*/
const payload = {
notification: {
title : "Tienes un nuevo seguidor !",
body: "Tu nuevo seguidor es .... ",
icon: "default"
}
};
/*
* Then using admin.messaging() we are sending the payload notification to the token_id of
* the device we retreived.
*/
return admin.messaging().sendToDevice(token_id, payload).then(response => {
return console.log('This was the notification Feature');
});
});
});
Ошибка
Невозможно прочитать свойство 'val' из undefined в exports.sendNotification.functions.database.ref.onWrite (/ user_code / index.js: 28: 20) на объекте.(/user_code/node_modules/firebase-functions/lib/cloud-functions.js:112:27) по следующему (нативному) адресу /user_code/node_modules/firebase-functions/lib/cloud-functions.js:28:71 в __awaiter(/user_code/node_modules/firebase-functions/lib/cloud-functions.js:24:12) в cloudFunction (/user_code/node_modules/firebase-functions/lib/cloud-functions.js:82:36) в / var /tmp / worker / worker.js: 716: 24 at process._tickDomainCallback (внутренняя / process / next_tick.js: 135: 7)