admin管理员组文章数量:1356292
I've been running into a problem with accessing data in a JS object. The code that gathers the data (from a Firebase database) is as follows:
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. Afterwards, when store_user_char() executes it should also print out each key from before.
The first console.log() outputs each key:object pair successfully as it writes into 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:
However the third console.log() never executes, and trying to get any data from char_obj by accessing a key like so:
char_obj['KSxpjEvkCOL6ugGkxqn']
does nothing and returns 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.
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.
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 ing up blank in trying to figure out whats going on, and how I can access the data.
Any help on the matter would be greatly appreciated!
EDIT: Full def of get_user_data is as follows:
// 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);
}
}
I've been running into a problem with accessing data in a JS object. The code that gathers the data (from a Firebase database) is as follows:
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. Afterwards, when store_user_char() executes it should also print out each key from before.
The first console.log() outputs each key:object pair successfully as it writes into 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:
However the third console.log() never executes, and trying to get any data from char_obj by accessing a key like so:
char_obj['KSxpjEvkCOL6ugGkxqn']
does nothing and returns 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.
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.
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 ing up blank in trying to figure out whats going on, and how I can access the data.
Any help on the matter would be greatly appreciated!
EDIT: Full def of get_user_data is as follows:
// 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);
}
}
Share
Improve this question
edited Oct 17, 2016 at 23:18
adjuremods
2,9982 gold badges14 silver badges17 bronze badges
asked Oct 17, 2016 at 12:34
shstyooshstyoo
1831 gold badge2 silver badges10 bronze badges
14
-
What is
user_char
exactly? Just a normal object? – MinusFour Commented Oct 17, 2016 at 12:43 - My guess is that hasOwnProperty() does not evaluate properties of char_obj that are inherited from user_char, since it doesn't evaluate inherited properties. – Feathercrown Commented Oct 17, 2016 at 12:44
- @Feathercrown char_obj = user_char creates a reference of user_char into char_obj, it has nothing to do with inheritance. though you are correct that hasOwnProperty() return false for inherited properties... – mannuscript Commented Oct 17, 2016 at 12:47
- Yes @MinusFour its initialized as var user_char = {}; – shstyoo Commented Oct 17, 2016 at 12:48
-
@shstyoo Then it doesn't have anything to evaluate!
var user_char = { bleh: "blah", pingas: ["snoo-","usual I see"], foo: "bar" } var char_obj = user_char; console.log(char_obj); for(var key in char_obj){ if(char_obj.hasOwnProperty(key)){ console.log(key); } }
works for me.... – Feathercrown Commented Oct 17, 2016 at 12:49
1 Answer
Reset to default 9getChar.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); inside callback of getChar and your problem will be solved. 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);
});
本文标签: firebaseHaving trouble iterating through Javascript objectsStack Overflow
版权声明:本文标题:firebase - Having trouble iterating through Javascript objects - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1743963410a2569445.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论