i'm using firebase cloud functions to listen to database value events , this is my code
var functions = require('firebase-functions');
var firebase = require('firebase-admin');
var serviceAccount = require("./serviceAccountKey.json");
firebase.initializeApp({
credential : firebase.credential.cert(serviceAccount),
databaseURL: "https://*******.firebaseio.com"
});
exports.notifications = functions.database.ref('/chat/{senderID}/{destinationID}/messages/{pushID}')
.onWrite(event => {
var eventSnapshot = event.data;
var sender = eventSnapshot.child('sender').val();
var message = eventSnapshot.child('message').val();
var destination = eventSnapshot.child('destination').val();
if (event.params.senderID === sender)
sendMessage(message,sender,destination);
}
);
function sendMessage(message, sender, destination) {
var senderUser = firebase.database().ref('users/'+sender+'/name');
var tokenUser = firebase.database().ref('fcm/'+destination+'/token');
tokenUser.once('value').then(function(tokenSnapshot) {
var token = tokenSnapshot.val();
console.log(token);
}, function (errorObject) {
console.log("The read failed: " + errorObject.code);
});
}
onWrite function execute normally , but tokenUser.once() is not executed.
event.params.senderID === sender. Put a few log statements in there with what they output so we can at least know what's happening when you run it. - Cedfcm/'+destination+'/token? - Cedconsole.log('my token is ', token);- Ced