Example
var ref = new Webcom("contacts").child("friends/john");
ref.once("value", snapshot => {
// Your code
// snapshot.val() returns the data associated with snapshot, that is, the data at the "friends/john" data node
});
Methods
acknowledged() → {boolean}
Returns the source of the data wrapped by this DataSnapshot
instance among. true
means the data is
acknowledged by the back end, false
means the data is purely local.
Returns:
- Type
- boolean
child(childPathString) → {DataSnapshot}
Retrieve child DataSnapshot
corresponding to specified relative path
Parameters:
Name | Type | Description |
---|---|---|
childPathString |
String | relative path |
Returns:
child DataSnapshot
- Type
- DataSnapshot
Example
// Get a reference to John
var ref = new Webcom("contacts").child("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)
});
eventType() → {string}
Returns the event type that produced this DataSnapshot instance, when it results from the Query.on
method. Otherwise, this property remains undefined.
Returns:
- Type
- string
exportVal() → {*}
Retrieve value object of the DataSnapshot
with all its metadata.
Returns:
DataSnapshot
value object with metadata
- Type
- *
Example
// Get a reference to our contacts
var ref = new Webcom("contacts").child("friends");
// Iterates over all friends with metadata
ref.set({ name: "John" }, function(error){
ref.on("value", function(snapshot) {
var user = snapshot.exportVal();
// Display information of current user
console.log(user.first_name);
//David
});
});
forEach(action) → {Boolean}
Iterate over DataSnapshot
's children
Parameters:
Name | Type | Description |
---|---|---|
action |
function | For each child, this function is called with child as parameter. You can return |
Returns:
true
if loop was stopped intentionally.
- Type
- Boolean
Example
// Get a reference to friends
var ref = new Webcom("contacts").child("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) → {Boolean}
Returns if specified child exists
Parameters:
Name | Type | Description |
---|---|---|
childPathString |
String | relative path |
Returns:
true
if child exists, false
otherwise
- Type
- Boolean
Example
// Get a reference to John
var ref = new Webcom("contacts").child("friends/john");
ref.once("value", function(snapshot){
var hasNameChild = snapshot.hasChild("name");
// hasNameChild ==> true
var hasMobilePhoneChild = snapshot.child("mobilePhone");
// hasMobilePhoneChild ==> false
});
hasChildren() → {Boolean}
Returns if current DataSnapshot
has at least one child.
Returns:
true
if any child exists, false
otherwise
- Type
- Boolean
Example
// Get a reference to John
var ref = new Webcom("contacts").child("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}
Retrieve key of current location targeted by DataSnapshot
Returns:
key name
- Type
- String
Examples
// Get a reference to John
var ref = new Webcom("contacts").child("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("contacts");
ref.once("value", function(snapshot){
var rootKey = snapshot.name();
// rootKey ==> null
var johnKey = snapshot.child("friends/john");
// lastKey ==> "john"
});
numChildren() → {Number}
Retrieve number of children
Returns:
number of children
- Type
- Number
Example
// Get a reference to John
var ref = new Webcom("contacts").child("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's a string therefore there is no children
});
rawVal() → {Object|String|Number|Boolean|Null}
Retrieves the raw value of this DataSnapshot
. The raw value is either a constant or
a treelike value involving exclusively JSON objects (with no JSON array at any level).
- See:
-
- val
Returns:
value object
- Type
- Object | String | Number | Boolean | Null
Example
// Get a reference to our contacts
var ref = new Webcom("contacts");
// Display all contacts
ref.once("value", function(snapshot) {
console.log(snapshot.rawVal());
// {"0": {"firstname": "john", "name": "doe"},
// "1": {"firstname": "david", "name": "smith"}}
});
ref() → {Webcom}
Retrieve current Webcom
instance for this DataSnapshot
.
Returns:
{Webcom} instance
- Type
- Webcom
Example
// Get a reference to John
var ref = new Webcom("contacts").child("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}
Retrieves the value of this DataSnapshot
as a JSON value. Subtrees whose keys are
integer values are returned as JSON arrays.
- See:
-
- rawVal
Returns:
value object
- Type
- Object | String | Number | Boolean | Null
Example
// Get a reference to our contacts
var ref = new Webcom("contacts");
// Display all contacts
ref.once("value", function(snapshot) {
console.log(snapshot.val());
// [{"firstname": "John", "name": "Doe"}, {"firstname": "Robert", "name": "Martin"}]
});