Firebase orderByChild query not returning ordered results

Firebase orderByChild query not returning ordered results,firebase,Firebase,I am trying to use the Firebase orderByChild() function per the documentation example but the queries are not coming back filtered or sorted. Here is the sample data structure: "notifications": { "001": { "member_id": "abc123", "created_at": 1424357680681, "new": true, "first_name": "Jon", "last_name": "Snow", "message": &qu

I am trying to use the Firebase

orderByChild()
function per the documentation example but the queries are not coming back filtered or sorted.

Here is the sample data structure:

"notifications": { 
  "001": { 
    "member_id":      "abc123",
    "created_at":     1424357680681,
    "new":            true,
    "first_name":     "Jon",
    "last_name":      "Snow",
    "message":        "likes your photo"
  },
  "002": { 
    "member_id":      "abc456",
    "created_at":     1424357680681,
    "new":            true,
    "first_name":     "Clark",
    "last_name":      "Kent",
    "message":        "likes your comment"
  }
}

When a user logs in, I want to query the notifications hash for only the notifications with a

member_id
that matches the current user's id.

I have tried both versions with and without the "child_added".

Option A:

  ref.orderByChild("member_id").equalTo(memberId);

Option B:

  ref.orderByChild("member_id").equalTo(memberId)
    .on("child_added", function(data) { 
       console.log("new child id = " + data.key());
    });

When I query the notifications hash, the query returns an array of all of the notifications. They are neither limited to the

member_id
of the current user, nor sorted by
member_id
. (I can delete the member_id key entirely and the notification will still be returned).

I can always filter the returned array by

member_id
in-app, but I would really like to get the query working per the documentation.

Any help is appreciated!


#1

I found the problem! I was under the impression that you could add the query modifiers in place.

I was doing:

  ref.orderByChild("member_id").equalTo(memberId);
  var sync = $firebase(ref); 

It should actually be:

  var sync = $firebase(ref.orderByChild("member_id").equalTo(memberId));

#2

Most likely your memberId variable is a number, while you're storing the corresponding value as a string.

If you store numbers as a string, this query won't give any results:

ref.orderByChild("member_id").equalTo(456)

But this one will:

ref.orderByChild("member_id").equalTo('456')

The easiest fix in your snippet of code is coercing the memberId to a string like this:

ref.orderByChild("member_id").equalTo(''+memberId)

See this fiddle for a working sample: http://jsfiddle.net/695rf0vy/


#3

Great that you found the solution Lena. Be sure to accept your self-answer once the system allows it. Sorry that I couldn't be more helpful. It would really help for your next question if you ensure that it contains a minimal, complete, verifiable example. I typically write those up in a jsfiddle or something like that, but most important is that it reproduces your actual problem. See stackoverflow.com/help/mcve

#4

Thanks for answering. The member_id's are definitely strings because they are alphanumeric. Sorry, I oversimplified for the example.

#5

In that case, please set up a reproduction of the problem. Feel free to modify the fiddle I posted into something that reproduces your problem. In general, posting an MCVE increases the chances that we can help you quickly (see stackoverflow.com/help/mcve).