title: How can I make this callback function only be called once? after my For loop
body: <p>The context is that I am trying to retrieve a list of records called <code>Posts</code> from a <code>firebase</code> database, first above I have gathered all the 'subcribed clubs' the user is in and put them in the array <code>SubscribedClubsList</code> in this instance the array contains the values <code>ALT5</code> and <code>BSM14</code> which are the keynames for the Clubs in the database.</p>
  
  <p><a href="https://i.stack.imgur.com/fkGgI.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/fkGgI.jpg" alt="enter image description here"></a></p>
  
  <p>the first <code>for loop</code> will go through all the items in <code>SubscribedClubList</code></p>
  
  <p>In my database the clubs have a designated space where they store <code>posts</code> as seen in the image. The function <code>getData</code> iterates through each post the club has made and stores it in the array <code>postArr</code>, after that I then <code>callback</code> the <code>array postArr</code> as I want to use this list of stored <code>posts</code> to display in a table on my webpage. </p>
  
  <p>As you can see in the database screenshot both of the <code>subscribed</code> societies have 3 posts each so the size of <code>postArr</code> is 6 </p>
  
  <p>However when I log it at my bottom on my code it and log a post from the 1st Club list like <code>console.log(arrNew[2])</code> it will log it twice, such as</p>
  
  <p><code>{Post: "meet and greet on friday", Timestamp: 1523298517138}
   {Post: "meet and greet on friday", Timestamp: 1523298517138}</code></p>
  
  <p>and when I log any post from the 2nd Club such as <code>console.log(arrNew[5])</code> or console.log[3] it will log similar to this:</p>
  
  <p><code>undefined
  {Post: "welcome", Timestamp: 1523359646325}</code>       </p>
  
  <p>How can I stop it logging twice like this and have the one time it logs be the full list as intended. </p>
  
  <p>I assume it is because the <code>callback</code> runs after the first iteration of the first <code>for loop</code> and returns the first 3 posts from the 1st club, then returns the 6 posts on the 2nd iteration.</p>
  
  <pre><code>        function getPosts(callback){
          var x = 0; 
          var postArr = [];
  
          for (var y = 0 ; y &lt; SubscripedClubsList.length ; y++){
  
           var ref = firebase.database().ref('ClubPosts').child(SubscripedClubsList[y]);
           ref.on('value', getData, errData);
  
          function getData(data){
              var posts = data.val();
              var keys = Object.keys(posts);
              for (var i = 0 ; i &lt; keys.length; i++){
                  var k = keys[i];
  
                  postArr[x] = posts[k];
                  x++;
  
              }
  
                  callback(postArr);
  
  
  
          }
  
          function errData(err){
              console.log('Error');
              console.log(err);
          }
  
       }
  
      }
  
     getPosts(function (arrNew){
      console.log(arrNew[2]);
     });
  </code></pre>