Examples
Simple query
const app = Webcom.App("contacts");
const ref = app.serverlessDb.root.relativeNode("friends/john");
ref.get().then(snapshot => {
// Your code
// snapshot.val() returns the data associated with snapshot, that is, the data at the "friends/john" data node
});
Subscription
const app = Webcom.App("contacts");
const ref = app.serverlessDb.root.relativeNode("friends/john");
ref.subscribe(Webcom.Event.ValueChange, Webcom.Callback(snapshot => {
// Your code
// snapshot.val() returns the data associated with snapshot, that is, the data at the "friends/john" data node
}));
Members
(readonly) acked :boolean
Indicates the source of the data wrapped by this ServerlessDb.DataSnapshot
instance: true
means the data has been
acknowledged by the back end, false
means the data is purely local.
Type:
- boolean
- Since:
- 3.0
(readonly) event :ServerlessDb.EventDescriptor
Gets the EventDescriptor
that produced this instance of
ServerlessDb.DataSnapshot
. May be undefined if it doesn't result from a notification (typically
when passed to the update method of a transaction
).
Type:
- Since:
- 3.0
(readonly) node :ServerlessDb.ServerlessDbNode
Gets the ServerlessDb.ServerlessDbNode
associated with this ServerlessDb.DataSnapshot
.
Type:
- Since:
- 3.0
Methods
child(childPath) → {ServerlessDb.DataSnapshot}
Retrieves the child of this ServerlessDb.DataSnapshot
relatively to a given path.
Parameters:
Name | Type | Description |
---|---|---|
childPath |
string | The relative path of the targeted |
Returns:
Example
const app = Webcom.App("contacts");
const ref = app.serverlessDb.root.relativeNode("friends/john");
ref.get().then(snapshot => {
const firstSnapshot = snapshot.child("0");
console.log(firstSnapshot.val());
// ==> { first: "John", last: "Doe" }
const firstName = snapshot.child("0/first").val();
// or
const firstName = firstSnapshot.child("first");
// or
const firstName = snapshot.child("0").child("first");
console.log(firstName.val());
// ==> John
const emptySnapshot = snapshot.child("fooBar");
console.log(emptySnapshot.val());
// ==> null (there is no "fooBar" child)
});
exportVal() → (nullable) {*}
Retrieves the value object associated with this ServerlessDb.DataSnapshot
, which includes all its metadata.
Returns:
- Type
- *
forEach(action) → {boolean}
Iterates over this ServerlessDb.DataSnapshot
's children.
Parameters:
Name | Type | Description |
---|---|---|
action |
function | This function is called for each |
Returns:
true
if the iteration was stopped intentionally before reaching the last child.
- Type
- boolean
Example
const app = Webcom.App("contacts");
const ref = app.serverlessDb.root.relativeNode("friends/john");
ref.get().then(snapshot => {
// Callback function will be called once per user found
snapshot.forEach(childSnapshot => {
const {firstName, lastName} = childSnapshot.val();
// e.g. { first: "John", last: "Doe" }
console.log("Found", firstName, lastName);
// return true to stop the enumeration (useful for search functions)
return true;
});
});
hasChild(childPathopt) → {boolean}
Returns if some child of this ServerlessDb.DataSnapshot
relatively to a given path exists.
Parameters:
Name | Type | Attributes | Description |
---|---|---|---|
childPath |
string |
<optional> |
If specified, relative path of the child to test. |
Returns:
- Type
- boolean
rawVal() → (nullable) {Object|string|number|boolean}
Retrieves the raw value associated with this ServerlessDb.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:
- Type
- Object | string | number | boolean
Example
const app = Webcom.App("contacts");
const ref = app.serverlessDb.root.relativeNode("friends");
ref.get().then(snapshot => {
console.log(snapshot.val());
// {"0": {"firstname": "John", "name": "Doe"},
// "1": {"firstname": "David", "name": "Smith"}}
});
val() → (nullable) {Object|Array|string|number|boolean}
Retrieves the value associated with this ServerlessDb.DataSnapshot
as a JSON value. Subtrees whose keys are
integers are returned as JSON arrays.
- See:
-
- rawVal
Returns:
- Type
- Object | Array | string | number | boolean
Example
const app = Webcom.App("contacts");
const ref = app.serverlessDb.root.relativeNode("friends");
ref.get().then(snapshot => {
console.log(snapshot.val());
// [{"firstname": "John", "name": "Doe"}, {"firstname": "David", "name": "Smith"}]
});