DocumentReference class

A DocumentReference refers to a document location in a Firestore database and can be used to write, read, or listen to the location. The document at the referenced location may or may not exist.

Signature:

export declare class DocumentReference<AppModelType = DocumentData, DbModelType extends DocumentData = DocumentData> 

Properties

Property Modifiers Type Description
converter FirestoreDataConverter<AppModelType, DbModelType> | null If provided, the FirestoreDataConverter associated with this instance.
firestore Firestore The Firestore instance the document is in. This is useful for performing transactions, for example.
id string The document's identifier within its collection.
parent CollectionReference<AppModelType, DbModelType> The collection this DocumentReference belongs to.
path string A string representing the path of the referenced document (relative to the root of the database).
type (not declared) The type of this Firestore reference.

Methods

Method Modifiers Description
fromJSON(firestore, json) static Builds a DocumentReference instance from a JSON object created by DocumentReference.toJSON().
fromJSON(firestore, json, converter) static Builds a DocumentReference instance from a JSON object created by DocumentReference.toJSON().
toJSON() Returns a JSON-serializable representation of this DocumentReference instance.
withConverter(converter) Applies a custom data converter to this DocumentReference, allowing you to use your own custom model objects with Firestore. When you call setDoc(), getDoc(), etc. with the returned DocumentReference instance, the provided converter will convert between Firestore data of type NewDbModelType and your custom type NewAppModelType.
withConverter(converter) Removes the current converter.

DocumentReference.converter

If provided, the FirestoreDataConverter associated with this instance.

Signature:

readonly converter: FirestoreDataConverter<AppModelType, DbModelType> | null;

DocumentReference.firestore

The Firestore instance the document is in. This is useful for performing transactions, for example.

Signature:

readonly firestore: Firestore;

DocumentReference.id

The document's identifier within its collection.

Signature:

get id(): string;

DocumentReference.parent

The collection this DocumentReference belongs to.

Signature:

get parent(): CollectionReference<AppModelType, DbModelType>;

DocumentReference.path

A string representing the path of the referenced document (relative to the root of the database).

Signature:

get path(): string;

DocumentReference.type

The type of this Firestore reference.

Signature:

readonly type = "document";

DocumentReference.fromJSON()

Builds a DocumentReference instance from a JSON object created by DocumentReference.toJSON().

Signature:

static fromJSON(firestore: Firestore, json: object): DocumentReference;

Parameters

Parameter Type Description
firestore Firestore The Firestore instance the snapshot should be loaded for.
json object a JSON object represention of a DocumentReference instance

Returns:

DocumentReference

an instance of DocumentReference if the JSON object could be parsed. Throws a FirestoreError if an error occurs.

DocumentReference.fromJSON()

Builds a DocumentReference instance from a JSON object created by DocumentReference.toJSON().

Signature:

static fromJSON<NewAppModelType = DocumentData, NewDbModelType extends DocumentData = DocumentData>(firestore: Firestore, json: object, converter: FirestoreDataConverter<NewAppModelType, NewDbModelType>): DocumentReference<NewAppModelType, NewDbModelType>;

Parameters

Parameter Type Description
firestore Firestore The Firestore instance the snapshot should be loaded for.
json object a JSON object represention of a DocumentReference instance
converter FirestoreDataConverter<NewAppModelType, NewDbModelType> Converts objects to and from Firestore.

Returns:

DocumentReference<NewAppModelType, NewDbModelType>

an instance of DocumentReference if the JSON object could be parsed. Throws a FirestoreError if an error occurs.

DocumentReference.toJSON()

Returns a JSON-serializable representation of this DocumentReference instance.

Signature:

toJSON(): object;

Returns:

object

a JSON representation of this object.

DocumentReference.withConverter()

Applies a custom data converter to this DocumentReference, allowing you to use your own custom model objects with Firestore. When you call setDoc(), getDoc(), etc. with the returned DocumentReference instance, the provided converter will convert between Firestore data of type NewDbModelType and your custom type NewAppModelType.

Signature:

withConverter<NewAppModelType, NewDbModelType extends DocumentData = DocumentData>(converter: FirestoreDataConverter<NewAppModelType, NewDbModelType>): DocumentReference<NewAppModelType, NewDbModelType>;

Parameters

Parameter Type Description
converter FirestoreDataConverter<NewAppModelType, NewDbModelType> Converts objects to and from Firestore.

Returns:

DocumentReference<NewAppModelType, NewDbModelType>

A DocumentReference that uses the provided converter.

DocumentReference.withConverter()

Removes the current converter.

Signature:

withConverter(converter: null): DocumentReference<DocumentData, DocumentData>;

Parameters

Parameter Type Description
converter null null removes the current converter.

Returns:

DocumentReference<DocumentData, DocumentData>

A DocumentReference<DocumentData, DocumentData> that does not use a converter.