0

I am creating a HTML page where a user can view all of their posts and edit them. At the moment I am just console logging the result to see if I get the desired data but I'm not receiving any errors or data.

The page should display only currently signed in user's posts but my code below isn't returning anything even though there are posts for that user in the Firebase database.

Any idea on how to only show the post for the currently signed in user?

Here is how my data is set up:

{
  "posts" : {
    "-KnP3MkFt5559uP1w5fh" : {
      "content" : "content 1",
      "title" : "title 1",
      "uid" : "O96P2INZn6S42dqqwgtz2OfgmYb2"
    },
    "-KnPPwHhfXDv34Lr0HMP" : {
      "content" : "content 2",
      "title" : "title 2",
      "uid" : "B3nHbd7G49Y2iwSA3tJSaHFVuCq2"
    },
    "-KnPR_ZsgOwrpRDGZkHT" : {
      "content" : "content 3",
      "title" : "title 3",
      "uid" : "O96P2INZn6S42dqqwgtz2OfgmYb2"
    }
  },
  "profiles" : {
    "B3nHbd7G49Y2iwSA3tJSaHFVuCq2" : {
      "email" : "-",
      "firstName" : "-",
      "lastName" : "-"
    },
    "O96P2INZn6S42dqqwgtz2OfgmYb2" : {
      "email" : "-",
      "firstName" : "firstFirst",
      "lastName" : "firstLast"
    }
  },
  "user-posts" : {
    "-KnP3MkFt5559uP1w5fh" : {
      "content" : "content 1",
      "title" : "title 1",
      "uid" : "O96P2INZn6S42dqqwgtz2OfgmYb2"
    },
    "-KnPPwHhfXDv34Lr0HMP" : {
      "content" : "content 2",
      "title" : "title 2",
      "uid" : "B3nHbd7G49Y2iwSA3tJSaHFVuCq2"
    },
    "-KnPR_ZsgOwrpRDGZkHT" : {
      "content" : "content 3",
      "title" : "title 3",
      "uid" : "O96P2INZn6S42dqqwgtz2OfgmYb2"
    }
  }
}

Here's my javascript:

firebase.auth().onAuthStateChanged(function (firebaseUser) {
    if (firebaseUser) {
var userid = firebase.auth().currentUser.uid;
var myPostsRef = firebase.database().ref('user-posts');

    myPostsRef.orderByChild('uid').equalTo('userid').on('child_added', function(snapshot) {
snapshot.forEach(function(snapshot) {
        var myPostData = snapshot.val();

        console.log(myPostData); 

    });
});
1
  • Well you are not catching any errors, so you wouldn't see them. The functions onAuthStateChanged and on('child_added' will both return an error callback, if you add something like .catch(function(error) { console.log(error)} you should see errors. Commented Jun 25, 2017 at 16:02

1 Answer 1

1

This:

myPostsRef.orderByChild('uid').equalTo('userid')

..looks like it should be this:

myPostsRef.orderByChild('uid').equalTo(userid)

Happens all the time :)

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks @pejalo & edzillion. It's working now. I also had to change this: myPostsRef.orderByChild('uid').equalTo(userid).once('value', function(snapshot) { snapshot.forEach(function(childSnapshot) { var myPostData = childSnapshot.val(); // changed from snapshot console.log(myPostData); });

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.