Matthew Nolan
Matthew Nolan

Reputation: 21

Firebase: Simply searching for a user

Please forgive me if this is a noob question. I am simply trying to search my collection for the existence of a user. I have been closely following the tutorial here, and I'm unable to produce his results.

var myFirebaseRef = new Firebase("https://myfirebase.firebaseio.com/pizza/users");

myFirebaseRef.set({
    "thisguy": {
        name: "Bill Smith",
        location: {
            city: "New York",
            state: "NY"
        },
        skill: "Code"
    },
    "someoneelse": {
        name: "Frankie Bones",
        location: {
            city: "Tampa",
            state: "FL"
        },
        skill: "Jeeps"
    }
});

myFirebaseRef.startAt('thisguy').endAt('thisguy').once('value', function(snap) {
    console.log('we found your guy ', snap.val())
});

MY hope is it would give me the result for "thisguy", but it does not.

BTW: I would prefer to use the push method when adding users to a database, but that is not what the tutorial illustrates. Where am I going wrong with all this? Thank you.

Upvotes: 0

Views: 65

Answers (1)

Rob DiMarco
Rob DiMarco

Reputation: 13266

Per the startAt() method docs, the behavior of startAt() and endAt() will depend on which orderBy*() method you've defined in your query.

In your query, you have not specified a default ordering, so it is using the Firebase default ordering of first priority and second key. In that context, the first argument of startAt() and endAt() is priority, and the second is key.

To do sorting by key, which is what you want in your case, try the following:

myFirebaseRef.orderByKey().equalTo('thisguy').once('value', function(snap) {
  console.log('we found your guy ', snap.val())
});

Upvotes: 2

Related Questions