DataSnapshot contains data from a Database location.
Any time you read data from the Database, you receive the data as a
DataSnapshot is passed to the event callbacks you attach
once(). You can extract the contents of the snapshot as a
val() method. Alternatively, you can
traverse into the snapshot by calling
child() to return child snapshots
(which you could then call
DataSnapshot is an efficiently generated, immutable copy of the data at
a Database location. It cannot be modified and will never change (to modify
data, you always call the
set() method on a
The key (last part of the path) of the location of this
The last token in a Database location is considered its key. For example,
"ada" is the key for the /users/ada/ node. Accessing the key on any
DataSnapshot will return the key for the location that generated it.
However, accessing the key on the root URL of a Database will return
Reference for the location that generated this
DataSnapshotfor the location at the specified relative path.
Passing a relative path to the
child()method of a DataSnapshot returns another
DataSnapshotfor the location at the specified relative path. The relative path can either be a simple child name (for example, "ada") or a deeper, slash-separated path (for example, "ada/name/first"). If the child location has no data, an empty
DataSnapshot(that is, a
DataSnapshotwhose value is
null) is returned.
A relative path to the location of child data.
Returns true if this
DataSnapshotcontains any data. It is slightly more efficient than using
snapshot.val() !== null.
exportVal()method is similar to
val(), except priority information is included (if available), making it suitable for backing up your data.
Enumerates the top-level children in the
val()is not guaranteed to match the ordering on the server nor the ordering of
child_addedevents. That is where
forEach()comes in handy. It guarantees the children of a
DataSnapshotwill be iterated in their query order.
If no explicit
orderBy*()method is used, results are returned ordered by key (unless priorities are used, in which case, results are returned by priority).
A function that will be called for each child DataSnapshot. The callback can return true to cancel further enumeration.
Returns boolean | void
true if enumeration was canceled due to your callback returning true.
Gets the priority value of the data in this
Applications need not use priority but can order collections by ordinary properties (see Sorting and filtering data).
Returns string | number | null
Returns true if the specified child path has (non-null) data.
A relative path to the location of a potential child.
trueif data exists at the specified child path; else
Returns whether or not the
DataSnapshothas any non-
You can use
hasChildren()to determine if a
DataSnapshothas any children. If it does, you can enumerate them using
forEach(). If it doesn't, then either this snapshot contains a primitive value (which can be retrieved with
val()) or it is empty (in which case,
true if this snapshot has any children; else false.
Returns the number of child properties of this
Returns a JSON-serializable representation of this object.
Returns Object | null
Depending on the data in a
val()method may return a scalar type (string, number, or boolean), an array, or an object. It may also return null, indicating that the
DataSnapshotis empty (contains no data).