cplus
cplus

Reputation: 1115

loop through firebase database object and ng-repeat the result

I am ng-repeating the objects stored in my $firebaseArrayobject.

mycontroller:

var projectquery = DatabaseRef.ref("projects").orderByKey().equalTo("-KUTPrs6ARZXjbjtNr7O");
$scope.projectslist = $firebaseArray(projectquery);

html view with ng-repeat:

<tr ng-repeat="obj in projectslist">
    <td>{{ obj.field1}}</td>
    <td>{{ obj.field2 }}</td>
    <td>{{ obj.field3 }}</td>
    <td>{{ obj.field4 }}</td>
</tr>

this is displaying only one project because I am passing in the project key manually to equalTo and thus filtering to show one.

From the other hand, I am getting a set of project keys from the below function by doing a forEach on another database where I get my project keys:

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 );
        });
    });

the output of the console is like:

my list is : -KUTLAZENGlxtzCtEfaZ
my list is : -KUTLM8r_kmVSTaxzLW5
my list is : -KUTPrs6ARZXjbjtNr7O

Question:

how is it possible to pass on the above list of project keys obtained from forEach into the first function which shows the projects and filters them by key?

Could somebody please give a relevant answer and help me solve this, without referring to other answers which are not directly related to my question?

thanks in advance!

edit:

my DatabaseRef refers to this factory, which is initializing the firebase database:

app.factory("DatabaseRef", function() {
    return firebase.database();
});

update:

After implementing Gourav's answer, my console shows undefined in place of the project IDs: enter image description here
as you can see, the user has six projects under his profile, but all of them are empty and undefined.

update 2:

here is console output after Gourav's edit:
enter image description here


and the error for ng-repeat:
enter image description here

Upvotes: 2

Views: 742

Answers (1)

Gourav Garg
Gourav Garg

Reputation: 2911

I don't know much about DatabaseRef but this might help.

$scope.projectslist = [];
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);
        var projectquery = DatabaseRef.ref("projects").orderByKey().equalTo(userprojects);
        $scope.projectslist.push(($firebaseArray(projectquery))[0]);
    });
});

Upvotes: 5

Related Questions