Go to console

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

  • isEqual(other: Query | null): boolean
  • Returns whether or not the current and provided queries represent the same location, have the same query parameters, and are from the same instance of firebase.app.App.

    Two Reference objects are equivalent if they represent the same location and are from the same instance of firebase.app.App.

    Two Query objects are equivalent if they represent the same location, have the same query parameters, and are from the same instance of firebase.app.App. Equivalent queries share the same sort order, limits, and starting and ending points.

    example
    var rootRef = firebase.database.ref();
    var usersRef = rootRef.child("users");
    
    usersRef.isEqual(rootRef);  // false
    usersRef.isEqual(rootRef.child("users"));  // true
    usersRef.parent.isEqual(rootRef);  // true
    example
    var rootRef = firebase.database.ref();
    var usersRef = rootRef.child("users");
    var usersQuery = usersRef.limitToLast(10);
    
    usersQuery.isEqual(usersRef);  // false
    usersQuery.isEqual(usersRef.limitToLast(10));  // true
    usersQuery.isEqual(rootRef.limitToLast(10));  // false
    usersQuery.isEqual(usersRef.orderByKey().limitToLast(10));  // false

    Parameters

    • other: Query | null

      The query to compare against.

    Returns boolean

    Whether or not the current and provided queries are equivalent.

limitToFirst

  • limitToFirst(limit: number): Query
  • Generates a new Query limited to the first specific number of children.

    The limitToFirst() method is used to set a maximum number of children to be synced for a given callback. If we set a limit of 100, we will initially only receive up to 100 child_added events. If we have fewer than 100 messages stored in our Database, a child_added event will fire for each message. However, if we have over 100 messages, we will only receive a child_added event for the first 100 ordered messages. As items change, we will receive child_removed events for each item that drops out of the active list so that the total number stays at 100.

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

    example
    // Find the two shortest dinosaurs.
    var ref = firebase.database().ref("dinosaurs");
    ref.orderByChild("height").limitToFirst(2).on("child_added", function(snapshot) {
      // This will be called exactly two times (unless there are less than two
      // dinosaurs in the Database).
    
      // It will also get fired again if one of the first two dinosaurs is
      // removed from the data set, as a new dinosaur will now be the second
      // shortest.
      console.log(snapshot.key);
    });

    Parameters

    • limit: number

      The maximum number of nodes to include in this query.

    Returns Query

limitToLast

  • limitToLast(limit: number): Query
  • Generates a new Query object limited to the last specific number of children.

    The limitToLast() method is used to set a maximum number of children to be synced for a given callback. If we set a limit of 100, we will initially only receive up to 100 child_added events. If we have fewer than 100 messages stored in our Database, a child_added event will fire for each message. However, if we have over 100 messages, we will only receive a child_added event for the last 100 ordered messages. As items change, we will receive child_removed events for each item that drops out of the active list so that the total number stays at 100.

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

    example
    // Find the two heaviest dinosaurs.
    var ref = firebase.database().ref("dinosaurs");
    ref.orderByChild("weight").limitToLast(2).on("child_added", function(snapshot) {
      // This callback will be triggered exactly two ti