Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/firebase/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Node.js Firebase函数如何从Firebase数据库获取布尔值_Node.js_Firebase_Firebase Realtime Database_Google Cloud Functions - Fatal编程技术网

Node.js Firebase函数如何从Firebase数据库获取布尔值

Node.js Firebase函数如何从Firebase数据库获取布尔值,node.js,firebase,firebase-realtime-database,google-cloud-functions,Node.js,Firebase,Firebase Realtime Database,Google Cloud Functions,我在Firebase函数中的node.js中有一段代码,我只想知道如何在类“Users”中的每个用户中获取变量“read”的布尔值 我的代码: let functions = require('firebase-functions'); let admin = require('firebase-admin'); admin.initializeApp(); exports.sendNotificationNewChat = functions.database.ref('/Usuario

我在Firebase函数中的node.js中有一段代码,我只想知道如何在类“Users”中的每个用户中获取变量“read”的布尔值

我的代码:

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函数的记录总是告诉我它获取空值
从“读取”中获取布尔值的部分代码:

snap.val()将返回一个对象,这样您就可以将该对象存储在一个变量中,然后尝试访问它,或者像下面这样尝试

admin.database().ref("/Usuarios/"+senderId+"/Chats/"+ChatId).once('value')           
.then(snap => {              
        if(snap.numChildren() === 0) // check for no data available              
        snap.child("read").val(); // instead of this line write like below
        const readChat =  **snap.val()[read];** or **snap.val().read**
       // if (readChat) return null
        console.log("readChat: ", readChat);
})
//考虑到这是你的结构

---user
-----senderId
--------Chats
-----------ChatId
---------------read: true

上述代码可以

您是否可以添加数据库的确切结构,如果可能,还可以导出数据(当您在RTDB模块上时,使用Firebase控制台右上角的三点按钮)
---user
-----senderId
--------Chats
-----------ChatId
---------------read: true