简体   繁体   English

Firebase中的嵌套查询,如何使用javascript显示恢复的数据?

[英]Nested queries in Firebase, how to display recovered data with javascript?

I have this structure in my database tree two nodes that contain the information of the messages of my web, in the node 'Messages' I have stored the information of all the messages and in the node 'User-messages' I have created a reference to These messages, for example, when a user sends a message, the key of that message is stored in this node (User-messages), and thus creates a list of the keys of all the messages that that user has sent, which Will serve to locate which messages belong to that particular user in the list of all sent messages 我在数据库树中有一个结构,其中两个节点包含我的Web消息的信息,在“消息”节点中,我存储了所有消息的信息,在“用户消息”节点中,我创建了一个引用以这些消息为例,例如,当用户发送消息时,该消息的密钥存储在此节点(用户消息)中,从而创建该用户已发送的所有消息的密钥列表,用于在所有已发送消息的列表中查找哪些消息属于该特定用户

tree example 树的例子

   var  RefMessage=new Firebase('https://chatfbexample.firebaseio.com/user-messages/332');
   var oMessages=[];
   var All= new Firebase('https://chatfbexample.firebaseio.com/all-messages');
   RefMessage.on("value",function(snapshot){

            snapshot.forEach(function(snap){
                var key=snap.key();
                var mensajeR=All.child(key);
              mensajeR.on('value',function(snap){
                var id=snap.val().id;
                oMessages[id]=[];
                oMessages[id]['id']=id;
               });

            });

        for(i in oMessages)
        {
            console.log(oMessages[i]['id']);
        }
        });

I store the data in arrays since I only want to get the last value (message) that has been stored,However, when I try to display the data with the loop it does not return anything to me, If I check the array, I see that the values have been stored, in fact, doing a console log without using the 'for' shows me the array, my question is, how can I display this data after retrieving it?? 因为我只想获取已存储的最后一个值(消息),所以我将数据存储在数组中。但是,当我尝试使用循环显示数据时,它不会返回任何信息,如果我检查数组,看到值已存储,实际上,在不使用'for'的情况下进行控制台日志显示了该数组,我的问题是,检索数据后如何显示该数据?

That's because firebase calls are run asynchronously and when your for loop executes it's not finished doing the calls. 这是因为firebase调用是异步运行的,并且当for循环执行时,它还没有完成调用。 Look into promises and then chaining off when your .on 's finish. 研究诺言,然后在.on完成时将其链接起来。

If you change your code to this: 如果将代码更改为此:

var RefMessage = new Firebase('https://chatfbexample.firebaseio.com/user-messages/332');
var oMessages = [];
var All = new Firebase('https://chatfbexample.firebaseio.com/all-messages');
RefMessage.on("value", function (snapshot) {

    snapshot.forEach(function (snap) {
        var key = snap.key();
        var mensajeR = All.child(key);
        mensajeR.on('value', function (snap) {
            console.log('done call');
            var id = snap.val().id;
            oMessages[id] = [];
            oMessages[id]['id'] = id;
        });

    });
    console.log('trying loop');

    for (i in oMessages) {
        console.log(oMessages[i]['id']);
    }
});

You'll see the 'trying loop' console log before the 'done call' one(s). 在“完成呼叫”之前,您会看到“尝试循环”控制台日志。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM