简体   繁体   中英

Firebase: How to extract values from snapshot object?

I am new in Firebase an am having a bad time trying to extract properties from snapshot object. As by the documentation, I should be able to pick directly the contents of the object this way:

snapshot.val().property_name

However, every time I try to do that I get an 'undefined' value. Yes, I know that the name is correct and that the property has contents.

If I do this:

MyRoom.update({Marker1:'foo'});

MyRoom.on('child_added', function(snapshot)
{
   alert(snapshot.name()); // it returns Marker1
   alert(snapshot.val());  // it returns foo
});

But if instead I try:

MyRoom.update({Marker1:'foo'});

MyRoom.on('child_added', function(snapshot)
{
   alert(snapshot.val().Marker1); // it returns undefined
});

What am I doing wrong?

tl;dr; you should be using MyRoom.on('value', for your case.

You typically store two basic types of data in Firebase:

  • objects
  • collections

Objects

Objects you typically store like this:

var MyRoom = new Firebase('your.firebaseio.com/someroom');
MyRoom.set({Name: 'This is my room', Owner: 'frank', Marker1:'bar'});

If you want to update/patch an object, you do how you do it above:

MyRoom.update({Marker1:'foo'});

To listen for changes to this specific room, you use:

MyRoom.on('value', function(snapshot) {
  var val = snapshot.val();
  alert(JSON.stringify(val)); // {Name: 'This is my room', Owner: 'frank', Marker1:'foo'}
  alert(val.Marker1); // foo
});

Here you always get the entire objects, even after you update a single property of it.

Collections

Collections are lists of objects. But collections are themselves also objects, so you could monitor a collection with on('value' . But that would mean that you constantly have to deal with the entire array, which most often is not practical.

You typically deal with the separate entries in the collection. Firebase has specific events for additions/deletions/updates to the collection. So to listen for new items that are added to a collection, you do:

var MyRooms = new Firebase('your.firebaseio.com/rooms');
MyRooms.push({Name: 'This is my room', Owner: 'frank', Marker1:'bar'});

MyRooms.on('child_added', function(snapshot) {
  alert(snapshot.val());
});

Collections are objects are collections are objects are...

Your confusion stems from the fact that you're mixing the collection logic and object logic from above. Although this is not often what you want, it is completely valid code, which is why it executes; just not with the result you were looking to get.

An example of how you might use "treat an object like a collection":

var MyRoom = new Firebase('your.firebaseio.com/someroom');

MyRoom.update({Marker2:'snafu'});

MyRooms.on('child_added', function(snapshot) {
  // somebody added a property to MyRoom
});

An example of when you might want to "treat a collection like an object": https://stackoverflow.com/a/25551254/209103

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM