1

I have flattening data in my firebase with the following code. But when I want display favorite user posts list with ng-repeat, the template gets repeated a second time and comes out totally blank. How can I correct this?

  "Post": {
    "xKkdfjjfld856i": {
      "name": "My first post",
      "author": "Miguel"
    },
    "xKkdfjj556FGHh": { ... },
    "xK499DF6FlHjih": { ... }
  },
  "users": {

    "John": {
      favorited_posts {
         "xKkdfjjfld856i": true,
         "xKkdfjj556FGHh": true
      }
    },
    "Mia": { ... },
    "Patrick": { ... }
  },

HTML:

<div ng-repeat="post in favoritedPosts track by $index">
    <div class="card post-card">
       <h1>{{post.name}}</h1>
       <p>{{post.author}}</p>
    </div> 
</div>

Controller :

var userRef = new Firebase("https://myApp.firebaseio.com/users")
var userFavoriteRef = userRef.child($scope.user_id).child("favorited_posts")
var favoritedPosts = $firebaseArray(userFavoriteRef)

userFavoriteRef.once("value", function(snapshot) {

  snapshot.forEach(function(childSnapshot) {

    var key = childSnapshot.key();

    var postRef = new Firebase("https://myApp.firebaseio.com/Posts")

    postRef.child(childSnapshot.ref().key()).once('value', function(postSnapshot) {
      console.log("Look",postSnapshot.val());
      $scope.favoritedPosts = postSnapshot.val();
    });

  });
});
adolfosrs
  • 9,286
  • 5
  • 39
  • 67
PalmThreeStudio
  • 489
  • 8
  • 21

1 Answers1

3

Try working with the $firebaseArray and $getRecord (documentation) to get the object value based on the object key. Then you will have everything you need without looping over assync calls.

Controller

var userRef = new Firebase("https://myApp.firebaseio.com/users")
var userFavoriteRef = userRef.child($scope.user_id).child("favorited_posts")
$scope.favoritedPosts = $firebaseArray(userFavoriteRef)
var postRef = new Firebase("https://myApp.firebaseio.com/Posts")
$scope.posts = $firebaseArray(postRef)

HTML

<div ng-repeat="post in favoritedPosts">
        <div class="card post-card">
            <h1>{{posts.$getRecord(post.$id).name}}</h1>
            <p>{{posts.$getRecord(post.$id).author}}</p>
        </div> 
</div>
adolfosrs
  • 9,286
  • 5
  • 39
  • 67
  • It's work like a charm ! You're the best thank you !! :D :D – PalmThreeStudio Jun 23 '16 at 17:14
  • 2
    @YngDev31 Uw! Really glad u made it :) – adolfosrs Jun 23 '16 at 17:14
  • @adolfosrs what if there are thousands posts at post node and you are downloading all posts to client? thats good thing? I dont think so – Zaid Mirza Oct 21 '16 at 19:49
  • @ZaidMirza you are godamn right. To avoid it you would rather prefer to set some query parameters in you call and you also should structure your database in a way it will best perform depending on your needs. :) – adolfosrs Oct 21 '16 at 21:55
  • @adolfosrs I found this solution is more applicable http://stackoverflow.com/questions/30299972/joining-data-between-paths-based-on-id-using-angularfire – Zaid Mirza Oct 22 '16 at 04:47