Class: DataSnapshot

DataSnapshot

DataSnapshot is useful to read data from a specific location. Callbacks passed to on() and once() are called with DataSnapshot instances as first parameter. To get data, use val() method.

DataSnapshot instance are immutables. To update data, use instead Webcom.set(), Webcom.update(), Webcom.push() or Webcom.remove() method.

Methods

child (childPathString)

api/DataSnapshot.js, line 141

Retrieve child DataSnapshot corresponding to specified relative path

Name Type Description
childPathString String

relative path

Returns:
Type Description
api.DataSnapshot child DataSnapshot
Example
// Get a reference to John
var ref = new Webcom("[[baseUrl]]/base/contacts/friends/john");

ref.once("value", function(snapshot){
 var nameSnapshot = snapshot.child("name");
 var name = nameSnapshot.val();
 console.log(name);
 // ==> { first: "John", last: "Doe" }

 var firstName = snapshot.child("name/first").val();
 //Or
 var firstName = nameSnapshot.child("first");
 // It's like you do snapshot.child("name").child("first");
 console.log(firstName.val());
 // ==> John

 var mobilePhone = snapshot.child("mobilePhone");
 console.log(mobilePhone.val());
 // ==> null (There is no "mobilePhone" child)
});

exportVal (){api.DataSnapshot}

api/DataSnapshot.js, line 112

Retrieve value object of the DataSnapshot with priority data.

Returns:
Type Description
api.DataSnapshot value object with priority data
Example
// Get a reference to our contacts
 var ref = new Webcom("[[baseUrl]]/base/contacts/friends");

 // Iterates over all friends with a priority
 ref.setWithPriority({ name: "John" }, 300, function(error){
     ref.on("value", function(snapshot) {
         var user = snapshot.exportVal();
         // Display information of current user
         console.log(user.first_name);
         //David
         console.log(user[".priority"];
         //300
     });
 });

forEach (action)

api/DataSnapshot.js, line 246

Iterate over DataSnapshot's children

Name Type Description
action function

For each child, this function is called with child as parameter. You can return true to stop loop.

Returns:
Type Description
Boolean true if loop was stopped intentionaly.
Example
// Get a reference to friends
var ref = new Webcom("[[baseUrl]]/base/contacts/friends");

ref.once("value", function(snapshot){
 // Callback function will get called once per user found
 snapshot.forEach(function(childSnapshot){
     // key contains key value of occurrence, per example "john"
     var key = childSnapshot.name();

     // childData will be current value of the child
     var childData = childSnapshot.val()
     // childData ==> { first: "John", last: "Doe" }

     //If you want you can cancel the enumeration with a return true
     //It's useful for a search function for example
     return true;
 });
});

hasChild (childPathString)

api/DataSnapshot.js, line 184

Returns if specified child exists

Name Type Description
childPathString String

relative path

Returns:
Type Description
Boolean true if child exists, false otherwise
Example
// Get a reference to John
var ref = new Webcom("[[baseUrl]]/base/contacts/friends/john");

ref.once("value", function(snapshot){
 var hasNameChild = snapshot.hasChild("name");
 // hasNameChild ==> true

 var hasMobilePhoneChild = snapshot.child("mobilePhone");
 // hasMobilePhoneChild ==> false
});

hasChildren (){Boolean}

api/DataSnapshot.js, line 288

Returns if current DataSnapshot has at least one child.

Returns:
Type Description
Boolean true if any child exists, false otherwise
Example
// Get a reference to John
var ref = new Webcom("[[baseUrl]]/base/contacts/friends/john");

ref.once("value", function(snapshot){
 var johnHasChildren = snapshot.hasChildren();
 // johnHasChildren ==> true

 var nameHasChildren = snapshot.child("name").hasChildren();
 // nameHasChildren ==> true

 var firstHasChildren = snapshot.child("name").child("first").hasChildren();
 // firstHasChildren ==> false ; a string doesn't have children
});

name (){String}

api/DataSnapshot.js, line 320

Retrieve key of current location targeted by DataSnapshot

Returns:
Type Description
String key name
Examples
// Get a reference to John
var ref = new Webcom("[[baseUrl]]/base/contacts/friends/john");

ref.once("value", function(snapshot){
 var johnKey = snapshot.name();
 // johnkey ==> "john"

 var lastKey = snapshot.child("name").child("last");
 // lastKey ==> "last"
});
//Other example without nodes
var ref = new Webcom("[[baseUrl]]/base/contacts");

ref.once("value", function(snapshot){
 var rootKey = snapshot.name();
 // rootkey ==> null

 var johnKey = snapshot.child("friends/john");
 // lastKey ==> "john"
});

numChildren (){Number}

api/DataSnapshot.js, line 356

Retrieve number of children

Returns:
Type Description
Number number of children
Example
// Get a reference to John
var ref = new Webcom("[[baseUrl]]/base/contacts/friends/john");

ref.once("value", function(snapshot){
 var johnChildren = snapshot.numChildren();
 // johnChildren ==> 1 ("name")

 var nameChildren = snapshot.child("name").numChildren();
 // nameChildren ==> 3 ("last","first","address")

 var firstChildren = snapshot.child("name").child("first").numChildren();
 // firstChildren ==> 0 ; It'a string therefore there isn't children
});

ref (){Webcom}

api/DataSnapshot.js, line 384

Retrieve current Webcom instance for this DataSnapshot.

Returns:
Type Description
Webcom {Webcom} instance
Example
// Get a reference to John
var ref = new Webcom("[[baseUrl]]/base/contacts/friends/john");

ref.once("value", function(snapshot){
 var snapshotRef = snapshot.ref();

 var snapshotRefJohn = snapshotRef.name();
 // snapshotRefJohn ==> "john"
 //snapshotRefJohn === snapshot.name()
});

val (){Object|String|Number|Boolean|Null}

api/DataSnapshot.js, line 86

Retrieve value object of the DataSnapshot

Returns:
Type Description
Object | String | Number | Boolean | Null value object
Example
// Get a reference to our contacts
 var ref = new Webcom("[[baseUrl]]/base/contacts/friends");

 // Iterates over all friends and display data
 ref.on("value", function(snapshot) {
     var user = snapshot.val();
     // Display information of current user
     console.log("First Name: " + user.name.first);
     console.log("Last Name: " + user.name.last);
 });