Go to console

A DataSnapshot contains data from a Database location.

Any time you read data from the Database, you receive the data as a DataSnapshot. A DataSnapshot is passed to the event callbacks you attach with on() or once(). You can extract the contents of the snapshot as a JavaScript object by calling the val() method. Alternatively, you can traverse into the snapshot by calling child() to return child snapshots (which you could then call val() on).

A 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 Reference directly).

Index

Properties

key

key: string | null

The key (last part of the path) of the location of this DataSnapshot.

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 null.

example
// Assume we have the following data in the Database:
{
  "name": {
    "first": "Ada",
    "last": "Lovelace"
  }
}

var ref = firebase.database().ref("users/ada");
ref.once("value")
  .then(function(snapshot) {
    var key = snapshot.key; // "ada"
    var childKey = snapshot.child("name/last").key; // "last"
  });
example
var rootRef = firebase.database().ref();
rootRef.once("value")
  .then(function(snapshot) {
    var key = snapshot.key; // null
    var childKey = snapshot.child("users/ada").key; // "ada"
  });

ref

The Reference for the location that generated this DataSnapshot.

Methods

child

  • child(path: string): DataSnapshot
  • Gets another DataSnapshot for the location at the specified relative path.