У меня есть код в node.js в функциях Firebase, который я просто хочу знать, как получить логическое значение переменной «read» для каждого пользователя в классе «Users».
Мой код:
let functions = require('firebase-functions');
let admin = require('firebase-admin');
admin.initializeApp();
exports.sendNotificationNewChat = functions.database.ref('/Usuarios/{usuarioId}/Chats/{chatsId}').onWrite((change, context) => {
//get the userId of the person receiving the notification because we need to get their token
//const receiverId = context.params.userId;
//console.log("receiverId: ", receiverId);
/*
// Only edit data when it is first created.
if (change.before.exists()) {
return null;
}*/
// Exit when the data is deleted.
if (!change.after.exists()) {
return null;
}
if (!change.after.exists()) {
return null;
}
//te escribe el json de el mensaje nuevo
const afterData = change.after.val();
console.log("afterData: ", afterData);
// const readChat = afterData.read;
// console.log("readChat: ", readChat);
//get the user id of the person who sent the message
const senderId = context.params.usuarioId;
console.log("senderId: ", senderId);
//get the user id of the person who sent the message
const receiverId = afterData.IdOtherUser;
console.log("receiverId: ", receiverId);
//get the message
const nameAd = afterData.nameAd;
console.log("nameAd: ", nameAd);
//get the message
const ChatId = context.params.chatsId;
console.log("ChatId: ", ChatId);
/*
//get the message id. We'll be sending this in the payload
const messageId = context.params.messageId;
console.log("messageId: ", messageId);
*/
return admin.database().ref("/Usuarios/"+senderId+"/Chats/"+ChatId).once('value').then(snap => {
const readChat = snap.child("read").val();
// if (readChat) return null
console.log("readChat: ", readChat);
//query the users node and get the name of the user who sent the message
return admin.database().ref("/Usuarios/" + senderId).once('value').then(snap => {
const senderName = snap.child("name").val();
console.log("senderName: ", senderName);
//get the token of the user receiving the message
return admin.database().ref("/Usuarios/" + receiverId).once('value').then(snap => {
const receiverName = snap.child("name").val();
console.log("receiverName: ", receiverName);
const token = snap.child("token").val();
console.log("token: ", token);
//we have everything we need
//Build the message payload and send the message
console.log("Construction the notification message.");
const payload = {
data: {
data_type: "direct_message",
title: "Nueva conversación de " + senderName,
message: "Selecciona para ver los mensajes",
}
};
return admin.messaging().sendToDevice(token, payload)
.then(function(response) {
return console.log("Successfully sent message:", response);
})
.catch(function(error) {
return console.log("Error sending message:", error);
});
});
});
});
});
Я использую эту часть кода, чтобы получить значение для чтения, но всегда записи функции firebase говорят мне, что она получает нулевое значение
Часть кода для получения логического значения из «read»:
admin.database().ref("/Usuarios/"+senderId+"/Chats/"+ChatId).once('value').then(snap => {
const readChat = snap.child("read").val();
// if (readChat) return null
console.log("readChat: ", readChat);