简体   繁体   English

无法迭代Javascript对象

[英]Having trouble iterating through Javascript objects

I've been running into a problem with accessing data in a JS object. 我一直遇到访问JS对象中的数据的问题。 The code that gathers the data (from a Firebase database) is as follows: 收集数据的代码(来自Firebase数据库)如下:

var get_user_data = function() {
    ...
    snapshot.forEach(function(child){
        var key = child.key;
        var value = child.val();
        user_char[key] = value;
        console.log(key, value);
    });
    store_user_char(user_char);
}

var store_user_char = function(user_char) {
    char_obj = user_char;
    console.log(char_obj);
    for(var key in char_obj){
        if(char_obj.hasOwnProperty(key)){
            console.log(key);
        }
}

Which should (in theory) create JS object from the Firebase database and while it is writing the data to user_char, it will print each key:object pair into the console. 哪个(理论上)应该从Firebase数据库创建JS对象,当它将数据写入user_char时,它会将每个键:对象对打印到控制台中。 Afterwards, when store_user_char() executes it should also print out each key from before. 之后,当store_user_char()执行时,它还应打印出之前的每个键。

The first console.log() outputs each key:object pair successfully as it writes into user_char. 第一个console.log()在写入user_char时成功输出每个键:对象对。 The second console.log() outputs the object successfully and I can even click and edit all the elements inside and looks like this in the Firefox console: 第二个console.log()成功输出对象,我甚至可以单击并编辑其中的所有元素,在Firefox控制台中如下所示:

对象属性

However the third console.log() never executes, and trying to get any data from char_obj by accessing a key like so: 但是第三个console.log()永远不会执行,并试图通过访问如下所示的键从char_obj获取任何数据:

char_obj['KSxpjEvkCOL6ugGkxqn']

does nothing and returns undefined . 什么都不做,返回undefined Oddly enough, clicking on the objects manually in Firefox allows me to parse each child element so I know the data is being stored somewhere. 奇怪的是,在Firefox中手动单击对象允许我解析每个子元素,因此我知道数据存储在某处。

The only thing I can think of is that the request from the database might take to long to return the data, but even then the store_user_char() function should execute after the data has been stored into user_char so I'm incredibly confused as to why my code can't seem to iterate through the object. 我唯一能想到的是来自数据库的请求可能需要很长时间来返回数据,但即使这样,store_user_char()函数也应该在数据存储到user_char之后执行,所以我非常困惑为什么我的代码似乎无法遍历对象。

I feel as though I'm missing something about JS objects which is the reason for why my code can't find the data, but I've been coming up blank in trying to figure out whats going on, and how I can access the data. 我觉得好像我遗漏了一些关于JS对象的东西,这就是为什么我的代码无法找到数据的原因,但是我一直在试图找出最新情况,以及如何访问数据。

Any help on the matter would be greatly appreciated! 任何有关此事的帮助将不胜感激!

EDIT: Full def of get_user_data is as follows: 编辑:get_user_data的完全def如下:

// Global variables
var user_email;
var user_char = {};
var uid;

var get_user_data = function() {
    // Authenticate current user
    var user = firebase.auth().currentUser;

    // Get User Email and char list
    if (user != null) {
        user_email = user.email;
        uid = firebase.auth().currentUser.uid;
        var getChar = firebase.database().ref('/users/' + uid + '/chars/').orderByKey();
        getChar.on('value', function(snapshot){
            snapshot.forEach(function(child){
                var key = child.key;
                var value = child.val();
                user_char[key] = value;
            });
        });
        store_user_char(user_char);
    }
}
getChar.on('value', function(snapshot){
    snapshot.forEach(function(child){
        var key = child.key;
        var value = child.val();
        user_char[key] = value;
    });
});

will be called asynchronously, put function call store_user_char(user_char); 将异步调用,put函数调用store_user_char(user_char); inside callback of getChar and your problem will be solved. 在getChar内部回调,你的问题将得到解决。 Like: 喜欢:

getChar.on('value', function(snapshot){
        snapshot.forEach(function(child){
            var key = child.key;
            var value = child.val();
            user_char[key] = value;
        });
       store_user_char(user_char);
    });

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

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