A DataSnapshot is an immutable representation of data at a specific location and point in time. Snapshots are returned by read operations such as once(), get(), and on() callbacks.
const snapshot = await db.ref('users/alice').get();
if (snapshot.exists()) {
console.log(snapshot.val());
}
Properties
| Property | Type | Description |
|---|
key | string | null | The key (last segment of the path) of the location this snapshot represents, or null for the root. |
ref | DatabaseReference | The DatabaseReference for the location this snapshot was read from. |
Methods
val
Returns the data contained in this snapshot as a JavaScript value (object, array, string, number, boolean, or null). Priority metadata (.priority) is stripped from the result.
Returns null if no data exists at this location.
const snapshot = await db.ref('users/alice').get();
const user = snapshot.val();
// { name: 'Alice', email: 'alice@example.com' }
exists
Returns true if this snapshot contains data (i.e., val() would return something other than null).
const snapshot = await db.ref('users/bob').get();
if (!snapshot.exists()) {
console.log('User not found');
}
hasChildren
Returns true if this snapshot has any child properties.
const snapshot = await db.ref('users/alice').get();
if (snapshot.hasChildren()) {
console.log(`Has ${snapshot.numChildren()} children`);
}
hasChild
hasChild(path: string): boolean
Returns true if a child exists at the specified relative path.
| Parameter | Type | Description |
|---|
path | string | The relative path to check. |
const snapshot = await db.ref('users/alice').get();
if (snapshot.hasChild('profile/avatar')) {
console.log('User has an avatar');
}
numChildren
Returns the number of immediate children of this snapshot.
const snapshot = await db.ref('users').get();
console.log(`Total users: ${snapshot.numChildren()}`);
child
child(path: string): DataSnapshot
Returns a DataSnapshot for a child location relative to this snapshot.
| Parameter | Type | Description |
|---|
path | string | The relative path to the child. |
const snapshot = await db.ref('users/alice').get();
const name = snapshot.child('name').val(); // 'Alice'
forEach
forEach(callback: (child: DataSnapshot) => boolean | void): void
Iterates over each direct child of this snapshot in sorted order. If the callback returns true, iteration stops early.
| Parameter | Type | Description |
|---|
callback | (child: DataSnapshot) => boolean | void | Called once for each child. Return true to stop iterating. |
const snapshot = await db.ref('users').get();
snapshot.forEach((childSnapshot) => {
console.log(`${childSnapshot.key}: ${childSnapshot.val().name}`);
});
Children are iterated in their sort order. If the snapshot was produced by a query with an orderBy clause, forEach respects that ordering.
getPriority
getPriority(): string | number | null
Returns the priority of the data at this location, or null if no priority is set.
const snapshot = await db.ref('scores/alice').get();
const priority = snapshot.getPriority(); // e.g. 100
exportVal
Returns the data including priority metadata (.priority and .value fields). Useful for serialization or backup scenarios where priority information must be preserved.
const snapshot = await db.ref('scores/alice').get();
const exported = snapshot.exportVal();
// { '.value': { score: 100 }, '.priority': 100 }
toJSON
Returns the same result as exportVal(). Provided for compatibility with JSON.stringify().
const snapshot = await db.ref('users/alice').get();
const json = JSON.stringify(snapshot); // uses toJSON() internally
isVolatile
Returns true if this snapshot was produced by a high-frequency volatile update. Volatile snapshots are delivered outside the normal consistency model for low-latency use cases such as cursor positions or live indicators.
db.ref('cursors').on('value', (snapshot) => {
if (snapshot.isVolatile()) {
// High-frequency update — render immediately
renderCursors(snapshot.val());
}
});
getServerTimestamp
getServerTimestamp(): number | null
Returns the server timestamp (in milliseconds since the Unix epoch) associated with this snapshot, or null if not available. Primarily useful for volatile snapshots to determine when the update was generated on the server.
db.ref('cursors').on('value', (snapshot) => {
const ts = snapshot.getServerTimestamp();
if (ts) {
console.log(`Server time: ${new Date(ts).toISOString()}`);
}
});