Ir a la consola

A Reference represents a specific location in your Database and can be used for reading or writing data to that Database location.

You can reference the root or child location in your Database by calling firebase.database().ref() or firebase.database().ref("child/path").

Writing is done with the set() method and reading can be done with the on() method. See Read and Write Data on the Web

Index

Properties

key

key: string | null

The last part of the Reference's path.

For example, "ada" is the key for https://<DATABASE_NAME>.firebaseio.com/users/ada.

The key of a root Reference is null.

example
// The key of a root reference is null
var rootRef = firebase.database().ref();
var key = rootRef.key;  // key === null
example
// The key of any non-root reference is the last token in the path
var adaRef = firebase.database().ref("users/ada");
var key = adaRef.key;  // key === "ada"
key = adaRef.child("name/last").key;  // key === "last"

parent

parent: Reference | null

The parent location of a Reference.

The parent of a root Reference is null.

example
// The parent of a root reference is null
var rootRef = firebase.database().ref();
parent = rootRef.parent;  // parent === null
example
// The parent of any non-root reference is the parent location
var usersRef = firebase.database().ref("users");
var adaRef = firebase.database().ref("users/ada");
// usersRef and adaRef.parent represent the same location

ref

Returns a Reference to the Query's location.

root

root: Reference

The root Reference of the Database.

example
// The root of a root reference is itself
var rootRef = firebase.database().ref();
// rootRef and rootRef.root represent the same location
example
// The root of any non-root reference is the root location
var adaRef = firebase.database().ref("users/ada");
// rootRef and adaRef.root represent the same location

Methods

child

  • child(path: string): Reference
  • Gets a Reference 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").

    example
    var usersRef = firebase.database().ref('users');
    var adaRef = usersRef.child('ada');
    var adaFirstNameRef = adaRef.child('name/first');
    var path = adaFirstNameRef.toString();
    // path is now 'https://sample-app.firebaseio.com/users/ada/name/first'

    Parameters

    • path: string

      A relative path from this location to the desired child location.

    Returns Reference

    The specified child location.

endAt

  • endAt(value: number | string | boolean | null, key?: string): Query
  • Creates a Query with the specified ending point.

    Using startAt(), endAt(), and equalTo() allows you to choose arbitrary starting and ending points for your queries.

    The ending point is inclusive, so children with exactly the specified value will be included in the query. The optional key argument can be used to further limit the range of the query. If it is specified, then children that have exactly the specified value must also have a key name less than or equal to the specified key.

    You can read more about endAt() in Filtering data.

    example
    // Find all dinosaurs whose names come before Pterodactyl lexicographically.
    var ref = firebase.database().ref("dinosaurs");
    ref.orderByKey().endAt("pterodactyl").on("child_added", function(snapshot) {
      console.log(snapshot.key);
    });

    Parameters

    • value: number | string | boolean | null

      The value to end at. The argument type depends on which orderBy*() function was used in this query. Specify a value that matches the orderBy*() type. When used in combination with orderByKey(), the value must be a string.

    • Optional key: string

      The child key to end at, among the children with the previously specified priority. This argument is only allowed if ordering by child, value, or priority.

    Returns Query

equalTo

  • equalTo(value: number | string | boolean | null, key?: string): Query
  • Creates a Query that includes children that match the specified value.

    Using startAt(), endAt(), and equalTo() allows us to choose arbitrary starting and ending points for our queries.

    The optional key argument can be used to further limit the range of the query. If it is specified, then children that have exactly the specified value must also have exactly the specified key as their key name. This can be used to filter result sets with many matches for the same value.

    You can read more about equalTo() in Filtering data.

    example
    // Find all dinosaurs whose height is exactly 25 meters.
    var ref = firebase.database().ref("dinosaurs");
    ref.orderByChild("height").equalTo(25).on("child_added", function(snapshot) {
      console.log(snapshot.key);
    });

    Parameters

    • value: number | string | boolean | null

      The value to match for. The argument type depends on which orderBy*() function was used in this query. Specify a value that matches the orderBy*() type. When used in combination with orderByKey(), the value must be a string.

    • Optional key: string

      The child key to start at, among the children with the previously specified priority. This argument is only allowed if ordering by child, value, or priority.

    Returns Query

isEqual