Erin B.
Erin B.

Reputation: 165

Querying Firebase database but not getting data or errors

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); 

    });
});

Upvotes: 0

Views: 67

Answers (1)

pejalo
pejalo

Reputation: 981

This:

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

..looks like it should be this:

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

Happens all the time :)

Upvotes: 1

Related Questions