jonny8bit
jonny8bit

Reputation: 25

Firebase: query returns null

I don't get why my query to my Firebase database doesn't work. I'm new to this and decided to follow a "simple tutorial" to read data. Writing a JSON object worked, was very happy to see that. Reading on the other hand starts to piss me off. The only thing that I wanted to do was read 1 "record" where the E-mail address was equal to some E-mail address. These are, and should be, unique in a user database. Once I retrieved the user, I could the use his UID, the automatically generated value on push();, to do other things. Hence this is a demo and I don't care about security a the moment, the default read and write behavior of a newly created Firebase database apply. Full read and write access from anywhere by anyone.

this is the code of the demo site to see if stuff works

<body>
    <h1>retrieve data</h1>
    <div id="aaa" style="margin:20px;padding:20px;border-radius:30px;background-color:#6666FF;float:left">USERADD</div>
    <div style="clear:both"></div>
    <pre id="rawdata"></pre>
</body>

This was the demo code that worked

var demofbdb = new Firebase("https://examples-sql-queries.firebaseio.com/user");

demofbdb
.startAt('[email protected]')
.endAt('[email protected]')
.once('value', function show(record) {
    $('#rawdata').text(JSON.stringify(record.val(), null, 4));
});

This is my code but returns null and I don't know why

var demofbdb = new Firebase("https://<my-firebase-database>.firebaseio.com/Users");

demofbdb
.startAt('[email protected]')
.endAt('[email protected]')
.once('value', function show(record) {
    $('#rawdata').text(JSON.stringify(record.val(), null, 4));
});

This is the structure of /Users

"-JlyxV5xvQFLybevk9kD" : {
    "AccountExpireDate" : "31/03/2016",
    "AccountStartDate" : "01/04/2015",
    "Alias" : "het hol van pluto",
    "Attended" : {
        "-JlyxMApRwcniJLXfqbU" : true
    },
    "Birthday" : "01/01/1970",
    "E-Mail" : "[email protected]",
    "FirstName" : "Demo",
    "LastName" : "De Demoon",
    "MemberOf" : [ "-JlyxMqpRw5nrJLef9bU" ],
    "OrganizationID" : "",
    "Password" : "abc123!"
},
"-JlyxSJ8MciBNxguT415" : {
    "AccountExpireDate" : "31/03/2016",
    "AccountStartDate" : "01/04/2015",
    "Alias" : "jos-het-debiele-ei",
    "Attended" : {
        "-JlyxMApRwcniJLXfqbU" : true
    },
    "Birthday" : "01/01/1970",
    "E-Mail" : "[email protected]",
    "FirstName" : "Jos",
    "LastName" : "Jossers",
    "MemberOf" : [ "-JlyxMqpRw5nrJLef9bU" ],
    "OrganizationID" : "",
    "Password" : "abc123!"
}

To clarify this structure, the "UIDs" are directly underneath Users. When the UIDs are unfolded, we see the details.

The expected result of my query is the one JSON object of the user who's first name is "Demo" and his last name is "De Demoon".

Thanks in advance

PS: I'm more familiar with SQL databases than noSQL. I'm kinda forced to use this last one besides the fact that I see noSQL databases more useful, and powerful, when we have "precomputed data", data that could be stored in a relational database but always needs to go trough the same joins in one way. Having these results calculated (joined) once and the results written to a noSQL database, makes lookups way faster by reducing unnecessary load and grouping together commonly requested datasets. Please also correct my vision if I'm wrong on this.

Upvotes: 2

Views: 2262

Answers (1)

Seamus
Seamus

Reputation: 4819

In the demo you were looking at, Michael Wulf used setPriority on each of the records as it was added. If you don't do that, then normally you would call orderByChild before startAt. In your case, this will do what you want:

var demofbdb = new Firebase("https://<my-firebase-database>.firebaseio.com/Users");

demofbdb
.orderByChild('E-Mail')
.startAt('[email protected]')
.endAt('[email protected]')
.once('value', function show(record) {
    $('#rawdata').text(JSON.stringify(record.val(), null, 4));
});

To setup the data like the demo, you could do something like this:

function createDemoData(){
    var users = {
        "123": {
            "email": "[email protected]",
            "name": "Kato"
        },
        "234": {
            "email": "[email protected]",
            "name": "Anant"
        },
        "345": {
            "email": "[email protected]",
            "name": "Michael"
        },
        "456": {
            "email": "[email protected]",
            "name": "Kavya"
        }
    };

    var fbdbref = new Firebase("https://<my-firebase-database>.firebaseio.com/user");
    fbdbref.set( users );

    fbdbref.child("123").setPriority("[email protected]");
    fbdbref.child("234").setPriority("[email protected]");
    fbdbref.child("345").setPriority("[email protected]");
    fbdbref.child("456").setPriority("[email protected]");
}

Now, you can query by email without using orderByChild, just like in the demo.

Upvotes: 5

Related Questions