Search code examples
javascriptangularjsfirebasefirebase-realtime-databaseangularfire

AngularFire getting data of child


Its pretty simple what I'm trying to do here: get data of the child. So in my case I want to get the value of the posts as described in my picture below:

screenshot

I actually got to the Posts and the username but now I dont know how to get to the ID. This is the code I did:

Ref.child("Posts").child(username).on("value", function(snapshot) {
              console.log(snapshot.val())
            }, function (errorObject) {
              console.log("The read failed: " + errorObject.code);
            });

But that gives me like the ID Object, now I need to specify the ID so i can get to the Posts value.


Solution

  • I resolved this through extensive "guess and check" methods I used. I just wanted to share the answer for those who are stuck with me now, and help future users identify their problem.

    So what I did here was attach a promise function that got the ID of the posts when I added the item. That was done by adding the parameter authData and going through a bunch of objects one by one using the dot-notation. So here is the code I was experimenting on to get the exact posts value for each post I make:

      postsValue = $firebaseArray(Ref.child("Posts").child(AuthService.User.username));
      postsValue.$add({
          Posts: $scope.textValue
      }).then(function(authData) {
          Ref.child("Posts").child(username).child(authData.path.o[2]).on("value", function(snapshot) {
              console.log(snapshot.val().Posts)
          }, function(errorObject) {
              console.log("The read failed: " + errorObject.code);
          });
      });