コンソールへ移動

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

ref

Methods

child

  • Gets another DataSnapshot for the location at the specified relative path.

    Passing a relative path to the child() method of a DataSnapshot returns another DataSnapshot 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 empty DataSnapshot (that is, a DataSnapshot whose value is null) is returned.

    example
    // Assume we have the following data in the Database:
    {
      "name": {
        "first": "Ada",
        "last": "Lovelace"
      }
    }
    
    // Test for the existence of certain keys within a DataSnapshot
    var ref = admin.database().ref("users/ada");
    ref.once("value")
      .then(function(snapshot) {
        var name = snapshot.child("name").val(); // {first:"Ada",last:"Lovelace"}
        var firstName = snapshot.child("name/first").val(); // "Ada"
        var lastName = snapshot.child("name").child("last").val(); // "Lovelace"
        var age = snapshot.child("age").val(); // null
      });

    Parameters

    • path: string

      A relative path to the location of child data.

    Returns DataSnapshot

    DataSnapshot for the location at the specified relative path.

exists

  • exists(): boolean
  • Returns true if this DataSnapshot contains any data. It is slightly more efficient than using snapshot.val() !== null.

    example
    // Assume we have the following data in the Database:
    {
      "name": {
        "first": "Ada",
        "last": "Lovelace"
      }
    }
    
    // Test for the existence of certain keys within a DataSnapshot
    var ref = admin.database().ref("users/ada");
    ref.once("value")
      .then(function(snapshot) {
        var a = snapshot.exists();  // true
        var b = snapshot.child("name").exists(); // true
        var c = snapshot.child("name/first").exists(); // true
        var d = snapshot.child("name/middle").exists(); // false
      });

    Returns boolean

    Whether this DataSnapshot contains any data.

exportVal

  • exportVal(): any
  • Exports the entire contents of the DataSnapshot as a JavaScript object.

    The exportVal() method is similar to val(), except priority information is included (if available), making it suitable for backing up your data.