DataSnapshot class Stay organized with collections Save and categorize content based on your preferences.
ADataSnapshot contains data from a Database location.
Any time you read data from the Database, you receive the data as aDataSnapshot. ADataSnapshot is passed to the event callbacks you attach withon() oronce(). You can extract the contents of the snapshot as a JavaScript object by calling theval() method. Alternatively, you can traverse into the snapshot by callingchild() to return child snapshots (which you could then callval() on).
ADataSnapshot 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 theset() method on aReference directly).
Signature:
exportdeclareclassDataSnapshotProperties
| Property | Modifiers | Type | Description |
|---|---|---|---|
| key | string | null | The key (last part of the path) of the location of thisDataSnapshot.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 anyDataSnapshot will return the key for the location that generated it. However, accessing the key on the root URL of a Database will returnnull. | |
| priority | string | number | null | Gets the priority value of the data in thisDataSnapshot.Applications need not use priority but can order collections by ordinary properties (seeSorting and filtering data ). | |
| ref | DatabaseReference | The location of this DataSnapshot. | |
| size | number | Returns the number of child properties of thisDataSnapshot. |
Methods
| Method | Modifiers | Description |
|---|---|---|
| child(path) | Gets anotherDataSnapshot for the location at the specified relative path.Passing a relative path to thechild() method of a DataSnapshot returns anotherDataSnapshot for 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 emptyDataSnapshot (that is, aDataSnapshot whose value isnull) is returned. | |
| exists() | Returns true if thisDataSnapshot contains any data. It is slightly more efficient than usingsnapshot.val() !== null. | |
| exportVal() | Exports the entire contents of the DataSnapshot as a JavaScript object.TheexportVal() method is similar toval(), except priority information is included (if available), making it suitable for backing up your data. | |
| forEach(action) | Enumerates the top-level children in theIteratedDataSnapshot.Because of the way JavaScript objects work, the ordering of data in the JavaScript object returned byval() is not guaranteed to match the ordering on the server nor the ordering ofonChildAdded() events. That is whereforEach() comes in handy. It guarantees the children of aDataSnapshot will be iterated in their query order.If no explicitorderBy*() method is used, results are returned ordered by key (unless priorities are used, in which case, results are returned by priority). | |
| hasChild(path) | Returns true if the specified child path has (non-null) data. | |
| hasChildren() | Returns whether or not theDataSnapshot has any non-null child properties.You can usehasChildren() to determine if aDataSnapshot has any children. If it does, you can enumerate them usingforEach(). If it doesn't, then either this snapshot contains a primitive value (which can be retrieved withval()) or it is empty (in which case,val() will returnnull). | |
| toJSON() | Returns a JSON-serializable representation of this object. | |
| val() | Extracts a JavaScript value from aDataSnapshot.Depending on the data in aDataSnapshot, theval() method may return a scalar type (string, number, or boolean), an array, or an object. It may also return null, indicating that theDataSnapshot is empty (contains no data). |
DataSnapshot.key
The key (last part of the path) of the location of thisDataSnapshot.
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 anyDataSnapshot will return the key for the location that generated it. However, accessing the key on the root URL of a Database will returnnull.
Signature:
getkey():string|null;DataSnapshot.priority
Gets the priority value of the data in thisDataSnapshot.
Applications need not use priority but can order collections by ordinary properties (seeSorting and filtering data ).
Signature:
getpriority():string|number|null;DataSnapshot.ref
The location of this DataSnapshot.
Signature:
readonlyref:DatabaseReference;DataSnapshot.size
Returns the number of child properties of thisDataSnapshot.
Signature:
getsize():number;DataSnapshot.child()
Gets anotherDataSnapshot for the location at the specified relative path.
Passing a relative path to thechild() method of a DataSnapshot returns anotherDataSnapshot for 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 emptyDataSnapshot (that is, aDataSnapshot whose value isnull) is returned.
Signature:
child(path:string):DataSnapshot;Parameters
| Parameter | Type | Description |
|---|---|---|
| path | string | A relative path to the location of child data. |
Returns:
DataSnapshot.exists()
Returns true if thisDataSnapshot contains any data. It is slightly more efficient than usingsnapshot.val() !== null.
Signature:
exists():boolean;Returns:
boolean
DataSnapshot.exportVal()
Exports the entire contents of the DataSnapshot as a JavaScript object.
TheexportVal() method is similar toval(), except priority information is included (if available), making it suitable for backing up your data.
Signature:
exportVal():any;Returns:
any
The DataSnapshot's contents as a JavaScript value (Object, Array, string, number, boolean, ornull).
DataSnapshot.forEach()
Enumerates the top-level children in theIteratedDataSnapshot.
Because of the way JavaScript objects work, the ordering of data in the JavaScript object returned byval() is not guaranteed to match the ordering on the server nor the ordering ofonChildAdded() events. That is whereforEach() comes in handy. It guarantees the children of aDataSnapshot will be iterated in their query order.
If no explicitorderBy*() method is used, results are returned ordered by key (unless priorities are used, in which case, results are returned by priority).
Signature:
forEach(action:(child:IteratedDataSnapshot)=>boolean|void):boolean;Parameters
| Parameter | Type | Description |
|---|---|---|
| action | (child:IteratedDataSnapshot) => boolean | void | A function that will be called for each child DataSnapshot. The callback can return true to cancel further enumeration. |
Returns:
boolean
true if enumeration was canceled due to your callback returning true.
DataSnapshot.hasChild()
Returns true if the specified child path has (non-null) data.
Signature:
hasChild(path:string):boolean;Parameters
| Parameter | Type | Description |
|---|---|---|
| path | string | A relative path to the location of a potential child. |
Returns:
boolean
true if data exists at the specified child path; elsefalse.
DataSnapshot.hasChildren()
Returns whether or not theDataSnapshot has any non-null child properties.
You can usehasChildren() to determine if aDataSnapshot has any children. If it does, you can enumerate them usingforEach(). If it doesn't, then either this snapshot contains a primitive value (which can be retrieved withval()) or it is empty (in which case,val() will returnnull).
Signature:
hasChildren():boolean;Returns:
boolean
true if this snapshot has any children; else false.
DataSnapshot.toJSON()
Returns a JSON-serializable representation of this object.
Signature:
toJSON():object|null;Returns:
object | null
DataSnapshot.val()
Extracts a JavaScript value from aDataSnapshot.
Depending on the data in aDataSnapshot, theval() method may return a scalar type (string, number, or boolean), an array, or an object. It may also return null, indicating that theDataSnapshot is empty (contains no data).
Signature:
val():any;Returns:
any
The DataSnapshot's contents as a JavaScript value (Object, Array, string, number, boolean, ornull).
Except as otherwise noted, the content of this page is licensed under theCreative Commons Attribution 4.0 License, and code samples are licensed under theApache 2.0 License. For details, see theGoogle Developers Site Policies. Java is a registered trademark of Oracle and/or its affiliates.
Last updated 2024-01-19 UTC.