1

I'm trying to read the data from firebase which I inserted using the push method in firebase.

Here is how I did dat:

var wishRef = new Firebase('https://burning-fire-1723.firebaseio.com/WishList');
wishRef.push({'user_id': user.email, 'text': wish});

which got inserted correctly. As per the documentation i'm trying to read data based on email as shown below:

new Firebase("https://burning-fire-1723.firebaseio.com/WishList")
.startAt('[email protected]')
.endAt('[email protected]')
.once('value', function (snap) {
    console.log('accounts matching email address', snap.val())
});

But all i get is null. Here is a DEMO

1
  • Ignore my previous answer (I deleted it). As far as I can see you're using once('value' where you should use once('child_added'. In addition you are trying to filter on a value other than name or priority. I'll see if I can make the fiddle work. Commented Jul 2, 2014 at 18:12

1 Answer 1

2

This is the code from your Fiddle:

new Firebase("https://burning-fire-1723.firebaseio.com/WishList")
    .startAt('[email protected]')
    .endAt('[email protected]')
    .once('value', show);

function show(snap) {
    console.log(snap.val());
    $('pre').text(JSON.stringify(snap.val(), null, 2));
}

And this is the data structure for your WishList in Firebase:

{
  "-JQnCbOF1MNdvKUCdDNB": {
    "text": "Climb Hill",
    "user_id": "[email protected]"
  },
  "-JQnDG8uWWB167BSSEEI": {
    "text": "Deep Sea Diving",
    "user_id": "[email protected]"
  },
  "-JQnDT7w1-K7VSnZNkVI": {
    "text": "KICK",
    "user_id": "[email protected]"
  }
}

It looks like you added these nodes with the Firebase push function. This function will generate a unique name for the node you added. The advantage of this is that you don't have to worry about coming up with a unique name. One disadvantage is that you have no control over the generated name: it's a meaningless blurb.

The problem is with the way you use startAt and endAt. The documentation for startAt says:

The starting point is specified using a priority and an optional child name. If no arguments are provided, the starting point will be the beginning of the data.

The value you are passing to startAt is neither a priority not a name in your Firebase.

If you comment out the startAt and endAt, you'll see all values show up in your pre:

new Firebase("https://burning-fire-1723.firebaseio.com/WishList")
    .once('value', show);
function show(snap) {
    console.log(snap.val());
    $('pre').text(JSON.stringify(snap.val(), null, 2));
}

One solution is to specify the email address as priority value when you add the data. See setPriority and setWithPriority.

Update

To set priorities on the existing nodes, I executed this function in your fiddle:

function setPriorities() {
    var list = new Firebase("https://burning-fire-1723.firebaseio.com/WishList");
    list.on('child_added', function(snap) {
        list.child(snap.name()).setPriority(snap.val().user_id);
    });
}

It loops over all the child nodes of your WishList and for each sets the priority. Note that you only need to run this function once, it just adds a priority to the data that is already there. Normally you would specify the priority when you add the element, so you wouldn't need to fix it later.

If you now re-visit your Fiddle, you'll see that it works.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.