Back to Firebase Js Sdk

DocumentReference class

docs-devsite/firestore_.documentreference.md

12.12.19.1 KB
Original Source

Project: /docs/reference/js/_project.yaml Book: /docs/reference/_book.yaml page_type: reference

{% comment %} DO NOT EDIT THIS FILE! This is generated by the JS SDK team, and any local changes will be overwritten. Changes should be made in the source code at https://github.com/firebase/firebase-js-sdk {% endcomment %}

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.

<b>Signature:</b>

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

Properties

PropertyModifiersTypeDescription
converterFirestoreDataConverter<!-- --><AppModelType, DbModelType> | nullIf provided, the <code>FirestoreDataConverter</code> associated with this instance.
firestoreFirestoreThe Firestore instance the document is in. This is useful for performing transactions, for example.
idstringThe document's identifier within its collection.
parentCollectionReference<!-- --><AppModelType, DbModelType>The collection this <code>DocumentReference</code> belongs to.
pathstringA 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

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

DocumentReference.converter

If provided, the FirestoreDataConverter associated with this instance.

<b>Signature:</b>

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

DocumentReference.firestore

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

<b>Signature:</b>

typescript
readonly firestore: Firestore;

DocumentReference.id

The document's identifier within its collection.

<b>Signature:</b>

typescript
get id(): string;

DocumentReference.parent

The collection this DocumentReference belongs to.

<b>Signature:</b>

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

DocumentReference.path

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

<b>Signature:</b>

typescript
get path(): string;

DocumentReference.type

The type of this Firestore reference.

<b>Signature:</b>

typescript
readonly type = "document";

DocumentReference.fromJSON()

Builds a DocumentReference instance from a JSON object created by DocumentReference.toJSON()<!-- -->.

<b>Signature:</b>

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

Parameters

ParameterTypeDescription
firestoreFirestoreThe Firestore instance the snapshot should be loaded for.
jsonobjecta JSON object represention of a <code>DocumentReference</code> instance

<b>Returns:</b>

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()<!-- -->.

<b>Signature:</b>

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

Parameters

ParameterTypeDescription
firestoreFirestoreThe Firestore instance the snapshot should be loaded for.
jsonobjecta JSON object represention of a <code>DocumentReference</code> instance
converterFirestoreDataConverter<!-- --><NewAppModelType, NewDbModelType>Converts objects to and from Firestore.

<b>Returns:</b>

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.

<b>Signature:</b>

typescript
toJSON(): object;

<b>Returns:</b>

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<!-- -->.

<b>Signature:</b>

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

Parameters

ParameterTypeDescription
converterFirestoreDataConverter<!-- --><NewAppModelType, NewDbModelType>Converts objects to and from Firestore.

<b>Returns:</b>

DocumentReference<!-- --><NewAppModelType, NewDbModelType>

A DocumentReference that uses the provided converter.

DocumentReference.withConverter()

Removes the current converter.

<b>Signature:</b>

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

Parameters

ParameterTypeDescription
converternull<code>null</code> removes the current converter.

<b>Returns:</b>

DocumentReference<!-- --><DocumentData<!-- -->, DocumentData<!-- -->>

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