如何使用firebase数组进行ng-repeat?

时间:2022-11-25 16:18:56

Context

In a Firebase DB I'm storing "events" and "users". Users can have favorite events, to manage them I only store the event's id in the favorite user's DB location. So to grab favorite events informations, I need to firstable grab the event id and then go to the DB events location, to collect all the datas I need.

在Firebase数据库中,我存储“事件”和“用户”。用户可以拥有喜欢的事件来管理它们我只将事件的id存储在收藏用户的DB位置。因此,为了获取最喜欢的事件信息,我需要首先获取事件ID,然后转到数据库事件位置,以收集我需要的所有数据。

如何使用firebase数组进行ng-repeat?

Problem

I would like to store in an Array all the favorite events informations (each event would be an Object with inside it : "key" : "value"), to use that Array in my HTML view and print the informations. But it doesn't work the way I coded it... :(

我想在一个数组中存储所有最喜欢的事件信息(每个事件都是一个内部的对象:“key”:“value”),在我的HTML视图中使用该数组并打印信息。但它不像我编码它的方式... :(

   // This ref is too grab favorite event id (in my case only 2) in the user DB location
    var refUserFavoris = firebase.database().ref().child("users/"+user.uid+"/events/favoris"); 
    $scope.favorisTmp = $firebaseArray(refUserFavoris);

    // This shows one array, with two objects (wich are my two user's favorite events) wich include ids
    console.log($scope.favorisTmp);

    // This is to load the objects and with the foreEach, grab there ids to use them in the next ref call
    $scope.favorisTmp.$loaded().then(function() 
    {
            angular.forEach($scope.favorisTmp, function(favoris) 
            {
                // This shows two lines : the id of each object
                console.log(favoris.$id);

                // Call a new ref to reach the event informations (in a different location of the DB) using the previous id
                firebase.database().ref("events/"+favoris.$id).once('value').then(function(snapshot) 
                {
                    // Attempt to store events datas for each id I have (in my case, only two)
                    snapshot.forEach(function(favorisSnap) 
                    {
                        var favSnap = favorisSnap.val();

                        // This shows a lot of "undefined" lines, wich I don't want. I would like two objects, with all informations inside
                        console.log(favSnap.nbPersonne);

                        // $scope.favorisF is an Array that I would like to use in a ng-repeat to print all datas for each event
                        // For now this doesn't show anything
                        $scope.favorisF = favSnap;
                    });

                    // If using favSnap out of the previous function, I got a "favSnap" is undifined error
                    console.log(favSnap);
                });
            });
    });
<ion-item ng-repeat="f in favorisF" class="item-avatar">
    {{f.nbPersonne}}
</ion-item>

EDIT 1 :

编辑1:

I tried a new way to have my data, but a new problem came, how to fill an Array inside a loop ? I've tried "push" and "$add" methods, but no one worked. Any ideas ?

我尝试了一种新方法来获取数据,但是出现了一个新问题,如何在循环中填充数组?我尝试过“push”和“$ add”方法,但没人工作。有任何想法吗 ?

        var newFav = [];
        var user;
        user = firebase.auth().currentUser;
        var refUserFavoris = firebase.database().ref().child("users/"+user.uid+"/events/favoris");
        
        $scope.favorisTmp = $firebaseArray(refUserFavoris);
        
        $scope.favorisTmp.$loaded().then(function()
        {
                angular.forEach($scope.favorisTmp, function(favoris) 
                {
                    console.log(favoris.$id);
                    var refFavoris = firebase.database().ref("events/"+favoris.$id);
                    refFavoris.on('value', function(snap)
                    {
                       //This is where I'm trying to fill "newFav" in each steps of the loop
                       newFav.push(snap.val());
                       console.log("Scope newFav vaut :", $scope.newFav);
                    });
                });
        });

2 个解决方案

#1


1  

I think you made a typo here.

我觉得你在这里打错了。

var refUserFavoris = firebase.database().ref("events/favoris/"+favoris.$id).once('value')

var refUserFavoris = firebase.database()。ref(“events / favoris /”+ favoris。$ id).once('value')

#2


1  

Thanks a lot Abdel, I fixed my problem :

非常感谢Abdel,我解决了我的问题:

Here is the solution

这是解决方案

$scope.newFav = [];
        console.log($scope.newFav);
        
        $scope.favorisTmp.$loaded().then(function()
        {
                angular.forEach($scope.favorisTmp, function(favoris) 
                {
                    console.log(favoris.$id);
                    var refFavoris = firebase.database().ref("events/"+favoris.$id);
                    refFavoris.on('value', function(snap)
                    {
                       $scope.newFav.push(snap.val());
                       console.log("Scope newFav vaut :", $scope.newFav);
                    });
                });
        });

#1


1  

I think you made a typo here.

我觉得你在这里打错了。

var refUserFavoris = firebase.database().ref("events/favoris/"+favoris.$id).once('value')

var refUserFavoris = firebase.database()。ref(“events / favoris /”+ favoris。$ id).once('value')

#2


1  

Thanks a lot Abdel, I fixed my problem :

非常感谢Abdel,我解决了我的问题:

Here is the solution

这是解决方案

$scope.newFav = [];
        console.log($scope.newFav);
        
        $scope.favorisTmp.$loaded().then(function()
        {
                angular.forEach($scope.favorisTmp, function(favoris) 
                {
                    console.log(favoris.$id);
                    var refFavoris = firebase.database().ref("events/"+favoris.$id);
                    refFavoris.on('value', function(snap)
                    {
                       $scope.newFav.push(snap.val());
                       console.log("Scope newFav vaut :", $scope.newFav);
                    });
                });
        });