title: AngularJS Firebase database query filter based on other database
body: <p>I get the values stored in the "projects" field under a current user's data in Firebase database called <strong>users</strong>:</p>
  
  <p><strong>first method:</strong></p>
  
  <pre><code>var promise = MyUser.user();
  promise.then( function onSuccess(user) {
      $scope.userprojectskeys  = user.projects;
  });
  </code></pre>
  
  <p><strong>second method:</strong></p>
  
  <pre><code>var keyquery = DatabaseRef.ref('/users/' + userId).orderByKey().once("value")
      .then(function onSuccess(snapshot) {
          snapshot.child("savedprojects").forEach(function(childSnapshot) {
              var userprojects = childSnapshot.val();
              console.log("my list is :", userprojects );
          });
      });
  </code></pre>
  
  <p>the <code>$scope.userprojectskeys</code> of the first method and the <code>userprojects</code> of the second method both return the data needed:<br /><br />
  <strong>first method output:</strong><br /><br />
  <a href="https://i.stack.imgur.com/I6rLx.png" rel="nofollow"><img src="https://i.stack.imgur.com/I6rLx.png" alt="enter image description here"></a>
  <br />
  <strong>second method output:</strong><br /></p>
  
  <pre><code>my list is : -KUTLAZENGlxtzCtEfaZ
  my list is : -KUTLM8r_kmVSTaxzLW5
  my list is : -KUTPrs6ARZXjbjtNr7O
  </code></pre>
  
  <p><br />On the other hand, I have another function which gets the list of children under another database called <strong>projects</strong> like the following:<br /></p>
  
  <pre><code>var projectquery = DatabaseRef.ref("projects").orderByKey().equalTo("-KUTPrs6ARZXjbjtNr7O");
  $scope.projectslist = $firebaseArray(projectquery);
  </code></pre>
  
  <p><br />what I am doing now is ng-repeating the fields of the object in <code>$scope.projectslist</code>, displaying the fields for each project.<br /></p>
  
  <pre><code>&lt;tr ng-repeat="obj in projectslist"&gt;
      &lt;td&gt;{{ obj.field1}}&lt;/td&gt;
      &lt;td&gt;{{ obj.field2 }}&lt;/td&gt;
      &lt;td&gt;{{ obj.field3 }}&lt;/td&gt;
      &lt;td&gt;{{ obj.field4 }}&lt;/td&gt;
  &lt;/tr&gt;
  </code></pre>
  
  <h2>Question :</h2>
  
  <p>how is it possible to pass the above list of project keys obtained previously from current user's data (either using first or second method) into the latter function which shows the projects and filters them by key in <code>equalTo()</code>?
  <br /></p>
  
  <p>but as you can see, I am passing one project ID manually to my filter <code>equalTo</code>, and it is working. But I need to pass in all of the keys into equalTo.
  <br /><br /></p>
  
  <h2>one proposed method is:</h2>
  
  <p>one proposed method is to combine these two functions like this:</p>
  
  <pre><code>$scope.projectslist = [];
  DatabaseRef.ref('/users/' + userId).orderByKey().once("value")
      .then(function onSuccess(snapshot) {
          snapshot.child("projects").forEach(function (childSnapshot) {
              var userprojects = childSnapshot.val();
              var projectquery = DatabaseRef.ref("projects").orderByKey().equalTo(userprojects);
              $scope.projectslist.push($firebaseArray(projectquery));
          });
      });
  </code></pre>
  
  <p>but this is with no success! the table is showing the rows, but no value inside: <a href="http://imgur.com/uByHT3c" rel="nofollow">http://imgur.com/uByHT3c</a> <br />
  here is the screenshot of the debugger: <a href="http://i.imgur.com/TaDZLlk.png" rel="nofollow">http://i.imgur.com/TaDZLlk.png</a>
  <br /></p>
  
  <h2>update:</h2>
  
  <p>here is the structure of my <code>users</code> child in <strong>mydatabase</strong> in firebase:<br />
  <a href="https://i.stack.imgur.com/KxTHK.png" rel="nofollow"><img src="https://i.stack.imgur.com/KxTHK.png" alt="enter image description here"></a>
  <br /> In which you can see five users, and for each user, there are a number of fields, one of which is a field that stores the user projects keys and in this example, the user has four project keys.</p>
  
  <p><br />
  and here is the projects child in firebase <strong>mydatabase</strong>:<br />
  <a href="https://i.stack.imgur.com/hSCSJ.png" rel="nofollow"><img src="https://i.stack.imgur.com/hSCSJ.png" alt="enter image description here"></a>
  <br /> here we see the global view of my firebase databse.
  So the goal is to look up the projects keys from <strong>users</strong> and then show the corresponding projects from the projects database, that is ng-repeat the fields of those projects.</p>
  
  <h2>update:</h2>
  
  <p>here is the console log after Zura's suggestion:</p>
  
  <pre><code>$scope.projectslist = [];
  DatabaseRef.ref('/users/' + userId).orderByKey().once("value")
      .then(function onSuccess(snapshot) {
          snapshot.child("projects").forEach(function (childSnapshot) {
              var userprojects = childSnapshot.val();
              //console.log("loaded. Data: ", userprojects);
              var projectquery = DatabaseRef.ref("projects").orderByKey().equalTo(userprojects);
  
              var list = ($firebaseArray(projectquery));
              console.log("list: ", list);
  
          });
      });
  </code></pre>
  
  <p><br />
  each list contains the info I need for each project of the current user. Perfect!
  But how to access them and store them in a <code>$scope</code>to be able to call it in my html?
  <br /></p>
  
  <p><a href="https://i.stack.imgur.com/izrb1.png" rel="nofollow"><img src="https://i.stack.imgur.com/izrb1.png" alt="enter image description here"></a></p>
  
  <p><br /></p>
  
  <h2>update:</h2>
  
  <p><br />I can only show the last project by passing the list into a <strong>$scope</strong>:<br />
  The project shown is the last of the <strong>forEach</strong> iteration. <strong>The question is how can I show all of the iterations of forEach and pass it into an array?</strong></p>
  
  <h2>update 25.10.2016:</h2>
  
  <p>here is an updated version of the code which concatenates the objects: first one, then the first two together, the first three, and at last all objects concatenated into one big array, see the console log below:</p>
  
  <pre><code> $scope.projectslist = [];
  
  DatabaseRef.ref('/users/' + userId).orderByKey().once("value")
      .then(function onSuccess(snapshot) {
          snapshot.child("projects").forEach(function (childSnapshot) {
              var userprojects = childSnapshot.val();
              var projectquery = DatabaseRef.ref("projects").orderByKey().equalTo(userprojects);
  
              var list = $firebaseArray(projectquery);
  
              $scope.list = $firebaseArray(projectquery);
              console.log("list: ", $scope.list);
              list.$loaded(function(){
                   $scope.projectslist = $scope.projectslist.concat(list);
                   console.log("loaded: ", $scope.projectslist);
              });
  
          });
      });
  </code></pre>
  
  <p><br />
  <a href="https://i.stack.imgur.com/9jEaf.png" rel="nofollow"><img src="https://i.stack.imgur.com/9jEaf.png" alt="enter image description here"></a>
  <br /> and each object directly contains the info I need as shown in the previous object screenshot.</p>