DocumentSnapshot class Stay organized with collections Save and categorize content based on your preferences.
ADocumentSnapshot contains data read from a document in your Firestore database. The data can be extracted with.data() or.get(<field>) to get a specific field.
For aDocumentSnapshot that points to a non-existing document, any data access will return 'undefined'. You can use theexists() method to explicitly verify a document's existence.
Signature:
exportdeclareclassDocumentSnapshot<AppModelType=DocumentData,DbModelTypeextendsDocumentData=DocumentData>Constructors
| Constructor | Modifiers | Description |
|---|---|---|
| (constructor)() | Constructs a new instance of theDocumentSnapshot class |
Properties
| Property | Modifiers | Type | Description |
|---|---|---|---|
| id | string | Property of theDocumentSnapshot that provides the document's ID. | |
| metadata | SnapshotMetadata | Metadata about theDocumentSnapshot, including information about its source and local modifications. | |
| ref | DocumentReference<AppModelType, DbModelType> | TheDocumentReference for the document included in theDocumentSnapshot. |
Methods
| Method | Modifiers | Description |
|---|---|---|
| data(options) | Retrieves all fields in the document as anObject. Returnsundefined if the document doesn't exist.By default,serverTimestamp() values that have not yet been set to their final value will be returned asnull. You can override this by passing an options object. | |
| exists() | Returns whether or not the data exists. True if the document exists. | |
| get(fieldPath, options) | Retrieves the field specified byfieldPath. Returnsundefined if the document or field doesn't exist.By default, aserverTimestamp() that has not yet been set to its final value will be returned asnull. You can override this by passing an options object. | |
| toJSON() | Returns a JSON-serializable representation of thisDocumentSnapshot instance. |
DocumentSnapshot.(constructor)
Constructs a new instance of theDocumentSnapshot class
Signature:
protectedconstructor();DocumentSnapshot.id
Property of theDocumentSnapshot that provides the document's ID.
Signature:
getid():string;DocumentSnapshot.metadata
Metadata about theDocumentSnapshot, including information about its source and local modifications.
Signature:
readonlymetadata:SnapshotMetadata;DocumentSnapshot.ref
TheDocumentReference for the document included in theDocumentSnapshot.
Signature:
getref():DocumentReference<AppModelType,DbModelType>;DocumentSnapshot.data()
Retrieves all fields in the document as anObject. Returnsundefined if the document doesn't exist.
By default,serverTimestamp() values that have not yet been set to their final value will be returned asnull. You can override this by passing an options object.
Signature:
data(options?:SnapshotOptions):AppModelType|undefined;Parameters
| Parameter | Type | Description |
|---|---|---|
| options | SnapshotOptions | An options object to configure how data is retrieved from the snapshot (for example the desired behavior for server timestamps that have not yet been set to their final value). |
Returns:
AppModelType | undefined
AnObject containing all fields in the document orundefined if the document doesn't exist.
DocumentSnapshot.exists()
Returns whether or not the data exists. True if the document exists.
Signature:
exists():thisisQueryDocumentSnapshot<AppModelType,DbModelType>;Returns:
this isQueryDocumentSnapshot<AppModelType, DbModelType>
DocumentSnapshot.get()
Retrieves the field specified byfieldPath. Returnsundefined if the document or field doesn't exist.
By default, aserverTimestamp() that has not yet been set to its final value will be returned asnull. You can override this by passing an options object.
Signature:
get(fieldPath:string|FieldPath,options?:SnapshotOptions):any;Parameters
| Parameter | Type | Description |
|---|---|---|
| fieldPath | string |FieldPath | The path (for example 'foo' or 'foo.bar') to a specific field. |
| options | SnapshotOptions | An options object to configure how the field is retrieved from the snapshot (for example the desired behavior for server timestamps that have not yet been set to their final value). |
Returns:
any
The data at the specified field location or undefined if no such field exists in the document.
DocumentSnapshot.toJSON()
Returns a JSON-serializable representation of thisDocumentSnapshot instance.
Signature:
toJSON():object;Returns:
object
a JSON representation of this object. Throws aFirestoreError if thisDocumentSnapshot has pending writes.
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 2025-06-30 UTC.