Using a string as a priority in Firebase
firebase, javascript
Solution
Looks like you're attempting to run these commands synchronously. At the time that you request rewards, there may not be any data yet (your push ops may not have finished).
Next, you should use setWithPriority, which will allow you to push the data and priority at the same time.
Last but not least, you haven't mentioned errors. I'll assume you checked those like any diligent dev would. In addition to the JS console, you can log the results of the callback functions (there's one for each of the methods you called, which could return an error if something went wrong).
So all together, it should look more like this:
var ref = new Firebase('https://<example>.firebaseio.com/rewards').push();
ref.setWithPriority({
name: 'apple',
otherKey: 'somevalue',
...
}, 'apple', function(err) {
if( error ) { console.error(err); }
else {
fetchValue();
}
});
function fetchValue() {
// wait for the set to complete before fetching data
new Firebase('https://<example>.firebaseio.com/rewards')
.startAt('apple')
.endAt('apple')
.once('value', function(snap) {
console.log('found:', snap.val()); // logs: "found null"
});
}
Problem
I read with interest the blog post here, which describes how to make a query equivalent to sql `WHERE email = x` ``` new Firebase("https://examples-sql-queries.firebaseio.com/user") .startAt('kato@firebase.com') .endAt('kato@firebase.com') .once('value', function(snap) { console.log('accounts matching email address', snap.val()) }); ``` I've tried to replicate this as follows: ``` root |-rewards |--JAJoFho0MYBMGNGrCdc |-name: "apple" |--JAJoFj7KsLSXMdGZ77V |-name: "orange" |--JAJoFp7HP6Ajq-VuMMx |-name: "banana" ``` There are many other fields in each rewards object... but I want to index the object by name and to be able to query all these objects to find the one matching a given name. The blog post instructs us to use setPriority() to achieve this. I have tried the following: ``` var ref = new Firebase('https://<example>.firebaseio.com/rewards').push({ name: 'apple', otherKey: 'somevalue', ... }); ref.setPriority('apple'); ``` If I then query firebase, it returns `null`: ``` new Firebase('https://<example>.firebaseio.com/rewards') .startAt('apple') .endAt('apple') .once('value', function(snap) { console.log('found:', snap.val()); // logs: "found null" }); ``` What am I doing wrong?