Index

Properties

key

key:string

OverridesDataSnapshot.key

ref

Inherited fromDataSnapshot.ref

TheReference for the location that generated thisDataSnapshot.

Methods

child

  • child(pathstring):DataSnapshot
  • Inherited fromDataSnapshot.child

    Gets anotherDataSnapshot for the location at the specified relative path.

    Passing a relative path to thechild() method of a DataSnapshot returnsanotherDataSnapshot for the location at the specified relative path. Therelative path can either be a simple child name (for example, "ada") or adeeper, slash-separated path (for example, "ada/name/first"). If the childlocation has no data, an emptyDataSnapshot (that is, aDataSnapshotwhose value isnull) 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 DataSnapshotvar ref = firebase.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.

    ReturnsDataSnapshot

exists

  • exists():boolean
  • Inherited fromDataSnapshot.exists

    Returns true if thisDataSnapshot contains any data. It is slightly moreefficient than usingsnapshot.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 DataSnapshotvar ref = firebase.database().ref("users/ada");ref.once("value")  .then(function(snapshot){var a = snapshot.exists();// truevar b = snapshot.child("name").exists();// truevar c = snapshot.child("name/first").exists();// truevar d = snapshot.child("name/middle").exists();// false  });

    Returnsboolean

exportVal

  • exportVal():any
  • Inherited fromDataSnapshot.exportVal

    Exports the entire contents of the DataSnapshot as a JavaScript object.

    TheexportVal() method is similar toval(), except priority informationis included (if available), making it suitable for backing up your data.

    Returnsany

    The DataSnapshot's contents as a JavaScript value (Object,Array, string, number, boolean, ornull).

forEach

  • forEach(action(aIteratedDataSnapshot) =>boolean |void):boolean
  • Inherited fromDataSnapshot.forEach

    Enumerates the top-level children in theDataSnapshot.

    Because of the way JavaScript objects work, the ordering of data in theJavaScript object returned byval() is not guaranteed to match the orderingon the server nor the ordering ofchild_added events. That is whereforEach() comes in handy. It guarantees the children of aDataSnapshotwill be iterated in their query order.

    If no explicitorderBy*() method is used, results are returnedordered by key (unless priorities are used, in which case, results arereturned by priority).

    example
    // Assume we have the following data in the Database:{"users": {"ada": {"first":"Ada","last":"Lovelace"    },"alan": {"first":"Alan","last":"Turing"    }  }}// Loop through users in order with the forEach() method. The callback// provided to forEach() will be called synchronously with a DataSnapshot// for each child:var query = firebase.database().ref("users").orderByKey();query.once("value")  .then(function(snapshot){    snapshot.forEach(function(childSnapshot){// key will be "ada" the first time and "alan" the second timevar key = childSnapshot.key;// childData will be the actual contents of the childvar childData = childSnapshot.val();  });});
    example
    // You can cancel the enumeration at any point by having your callback// function return true. For example, the following code sample will only// fire the callback function one time:var query = firebase.database().ref("users").orderByKey();query.once("value")  .then(function(snapshot){    snapshot.forEach(function(childSnapshot){var key = childSnapshot.key;// "ada"// Cancel enumerationreturntrue;  });});

    Parameters

    Returnsboolean

    true if enumeration was canceled due to your callbackreturning true.

getPriority

hasChild

  • hasChild(pathstring):boolean
  • Inherited fromDataSnapshot.hasChild

    Returns true if the specified child path has (non-null) data.

    example
    // Assume we have the following data in the Database:{"name": {"first":"Ada","last":"Lovelace"  }}// Determine which child keys in DataSnapshot have data.var ref = firebase.database().ref("users/ada");ref.once("value")  .then(function(snapshot){var hasName = snapshot.hasChild("name");// truevar hasAge = snapshot.hasChild("age");// false  });

    Parameters

    • path:string

      A relative path to the location of a potential child.

    Returnsboolean

    true if data exists at the specified child path; elsefalse.

hasChildren

  • hasChildren():boolean
  • Inherited fromDataSnapshot.hasChildren

    Returns whether or not theDataSnapshot has any non-null childproperties.

    You can usehasChildren() to determine if aDataSnapshot has anychildren. If it does, you can enumerate them usingforEach(). If itdoesn't, then either this snapshot contains a primitive value (which can beretrieved withval()) or it is empty (in which case,val() will returnnull).

    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 a = snapshot.hasChildren();// truevar b = snapshot.child("name").hasChildren();// truevar c = snapshot.child("name/first").hasChildren();// false  });

    Returnsboolean

    true if this snapshot has any children; else false.

numChildren

  • numChildren():number
  • Inherited fromDataSnapshot.numChildren

    Returns the number of child properties of thisDataSnapshot.

    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 a = snapshot.numChildren();// 1 ("name")var b = snapshot.child("name").numChildren();// 2 ("first", "last")var c = snapshot.child("name/first").numChildren();// 0  });

    Returnsnumber

toJSON

val

  • val():any
  • Inherited fromDataSnapshot.val

    Extracts a JavaScript value from aDataSnapshot.

    Depending on the data in aDataSnapshot, theval() method may return ascalar type (string, number, or boolean), an array, or an object. It may alsoreturn null, indicating that theDataSnapshot is empty (contains no data).

    example
    // Write and then read back a string from the Database.ref.set("hello")  .then(function(){return ref.once("value");  })  .then(function(snapshot){var data = snapshot.val();// data === "hello"  });
    example
    // Write and then read back a JavaScript object from the Database.ref.set({name:"Ada",age:36 })  .then(function(){return ref.once("value");  })  .then(function(snapshot){var data = snapshot.val();// data is { "name": "Ada", "age": 36 }// data.name === "Ada"// data.age === 36  });

    Returnsany

    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 2023-09-28 UTC.