Reputation: 498
I have two different array in Firebase : The first is "General post" and the second is "The favorite post by user".
When user want put a post in favorite, I copy all data of this unique post in "General post" to "The favorite post by user" with the same ID.
The problem is when the post name is changed, he is changed only in "General post" and stay the same in "The favorite post by user".
So I try to retrieve only post name in "General post" like this :
var postRef = new Firebase("https://myapp.firebaseio.com/posts")
var userFavoriteRef = userRef.child($scope.user_id).child("favorite_posts")
$scope.favoritePosts = $firebaseArray(userFavoriteRef)
var favoritePosts = $firebaseArray(userFavoriteRef)
userFavoriteRef.once("value", function(snapshot) {
snapshot.forEach(function(childSnapshot) {
var key = childSnapshot.key()
$scope.key = childSnapshot.key()
postRef.child($scope.key).child("name").once('value', function(snap) {
console.log(snap.val())
$scope.name = snap.val()
})
})
})
But I have "null" in my console log... And it's dont work ...
Maybe my logic is false ...
What do you think ? Pending read you ... Thanks !
Upvotes: 1
Views: 135
Reputation: 552
Might be better to take a different approach to storing the data. Instead of having two copies of the data, just have one with a unique id. Then on each individual user, you can reference the unique ids of the posts they have favorited. This allows you to be able to update the data only in the one location.
For example, you could use a data structure like:
myFirebase
- users
-user1
-user2
-user3
-favorites
-unique2 (reference to post, not the actual post)
-unique4
- posts
-unique1
-unique2
-unique3
-unique4
So, you just have one copy of the data and then reference it from your user objects to retrieve what you are looking for. And changes to the post are reflected everywhere that references that post.
Upvotes: 1