Class: DataSnapshot

ServerlessDb.DataSnapshot

This class represents the data associated with a node of the Webcom Serverless Database service. Instances of this class are built by the Webcom SDK when querying or subscribing a data node:

To get the data associated with such an instance, use the ServerlessDb.DataSnapshot#val method.

Instances of this class are immutable. To update data within the database, use the ServerlessDb.ServerlessDbNode#set, ServerlessDb.ServerlessDbNode#merge, ServerlessDb.ServerlessDbNode#push or ServerlessDb.ServerlessDbNode#clear methods.

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) childCount

Gets the number of children of this ServerlessDb.DataSnapshot.

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

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 ServerlessDb.DataSnapshot child.

Returns:
Type
ServerlessDb.DataSnapshot
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 ServerlessDb.DataSnapshot child (passed as a unique parameter of type ServerlessDb.DataSnapshot). It may return either true to stop the iteration or nothing to continue.

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.
Since 3.0, if not specified, tests whether at least one child exists.

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"}]
});
show
deprecated