0

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
        });
joemost45
  • 45
  • 5
  • 3
    If you're "pulling it down" it's probably an asynchronous request, and you're accessing it before the asynchronous request has completed. Hard to say without more context. – Dave Newton Aug 08 '16 at 02:58
  • Dave is likely right: you're seeing effects of the fact that Firebase loads the data asynchronously. But it's hard to help you in the current format. Can you reduce the question to a single [code sample that doesn't work the way you expect it to work](http://stackoverflow.com/help/mcve). Then we can focus on explaining what happens in there. – Frank van Puffelen Aug 08 '16 at 04:05
  • man I feel like an idiot, i should have known that.. Ill add en update right now – joemost45 Aug 09 '16 at 00:12
  • I guess my real question is how can I control this code to not set each array until each query request is complete? Thanks so much for your help! @DaveNewton and Frank van Puffelen – joemost45 Aug 09 '16 at 00:17

0 Answers0