This might be a very odd question and I am not entirely sure how to phrase it. I am making a call to pull down some nosql data, in this particulary case, firebase (not really important to this though)
var testarray = [];
this.groupsRef = this.getRef().child('groups').orderByChild('id').equalTo(groups[i]);
this.groupsRef.on('value', (snap1) => {
snap1.forEach((child1) => {
testarray.push(child1.val())
});
});
Each call returns a 'Object' type, so simply my idea is to stash it in an array.
However, for some reason I am not able to do testarray[0] as it just always returns undefined. However!, if I define the array staticly such as
var myArray= []
var ob1 = {name: "Bob", car: "Ford"};
myArray.push(ob1)
so if I do testarray[0] that will return.
Upon looking into google chromes developer consule, where I was printing both of these arrays, I noticed the statically generated array would say "[Object]" while the other array would just appear in the console like "[]" despite having multiple entries in that array. I am begining to think this has to do with some of the issue as to why any return I attempt to make from the array is undefed
Any help would be lifesaving! Thanks
UPDATE: So I guesss it would make sense I am setting stuff before I have returned the values;
var groups = []
//get user id
var user = firebase.auth().currentUser.uid;
this.itemsRef = this.getRef().child('members').orderByChild('uid').equalTo(user);
this.itemsRef.on('value', (snap) => {
// get children as an array
snap.forEach((child) => {
//Opulates the array created above
group = child.val().groups.groupID
completedGroups = true
items.push(child.val())
});
});
var returnedGroups = [];
//we returned an array of groups, now need to make multiple calls to get each groups object
for(var i = 0; i < groups.length; i++ ){
this.groupsRef = this.getRef().child('groups').orderByChild('id').equalTo(groups[i]);
this.groupsRef.on('value', (snap1) => {
snap1.forEach((child1) => {
//add it to our array that we will use to display
items.push(child1.val())
});
});
}
//now lets just set our array
//UPDATE: I believe this is running before all our querys are complete
this.setState({
db: returnedGroups,
dataSource: this.state.dataSource.cloneWithRows(returnedGroups),
loaded: true
});