A DocumentSnapshot is an immutable representation for a document in a Firestore database. The data can be extracted with [data()] or [get(fieldPath)] to get a specific field.
For a DocumentSnapshot that points to a non-existing document, any data access will return 'undefined'. You can use the [exists] property to explicitly verify a document's existence.
DocumentSnapshot
Package
@google-cloud/firestore!Constructors
(constructor)(ref, _fieldsProto, readTime, createTime, updateTime)
constructor(ref: DocumentReference<T>, _fieldsProto?: ApiMapValue | undefined, readTime?: Timestamp, createTime?: Timestamp, updateTime?: Timestamp);
Constructs a new instance of the DocumentSnapshot
class
Name | Description |
ref |
DocumentReference<T>
The reference to the document. |
_fieldsProto |
ApiMapValue | undefined
The fields of the Firestore |
readTime |
Timestamp
The time when this snapshot was read (or undefined if the document exists only locally). |
createTime |
Timestamp
The time when the document was created (or undefined if the document does not exist). |
updateTime |
Timestamp
The time when the document was last updated (or undefined if the document does not exist). |
Properties
_fieldsProto
readonly _fieldsProto?: ApiMapValue | undefined;
Type | Description |
ApiMapValue | undefined |
createTime
get createTime(): Timestamp | undefined;
The time the document was created. Undefined for documents that don't exist.
{Timestamp|undefined} DocumentSnapshot#createTime
Type | Description |
Timestamp | undefined |
exists
get exists(): boolean;
True if the document exists.
{boolean} DocumentSnapshot#exists
Type | Description |
boolean |
id
get id(): string;
The ID of the document for which this DocumentSnapshot contains data.
{string} DocumentSnapshot#id
Type | Description |
string |
readTime
get readTime(): Timestamp;
The time this snapshot was read.
{Timestamp} DocumentSnapshot#readTime
Type | Description |
Timestamp |
ref
get ref(): DocumentReference<T>;
A [DocumentReference]DocumentReference for the document stored in this snapshot.
{DocumentReference} DocumentSnapshot#ref
Type | Description |
DocumentReference<T> |
updateTime
get updateTime(): Timestamp | undefined;
The time the document was last updated (at the time the snapshot was generated). Undefined for documents that don't exist.
{Timestamp|undefined} DocumentSnapshot#updateTime
Type | Description |
Timestamp | undefined |
Methods
data()
data(): T | undefined;
Retrieves all fields in the document as an object. Returns 'undefined' if the document doesn't exist.
Type | Description |
T | undefined | {T|undefined} An object containing all fields in the document or 'undefined' if the document doesn't exist. |
fromObject(ref, obj)
static fromObject<U>(ref: firestore.DocumentReference<U>, obj: firestore.DocumentData): DocumentSnapshot<U>;
Creates a DocumentSnapshot from an object.
Name | Description |
ref |
FirebaseFirestore.DocumentReference<U>
The reference to the document. |
obj |
firestore.DocumentData
The object to store in the DocumentSnapshot. The created DocumentSnapshot. |
Type | Description |
DocumentSnapshot<U> |
Name | Description |
U |
fromUpdateMap(ref, data)
static fromUpdateMap<U>(ref: firestore.DocumentReference<U>, data: UpdateMap): DocumentSnapshot<U>;
Creates a DocumentSnapshot from an UpdateMap.
This methods expands the top-level field paths in a JavaScript map and turns { foo.bar : foobar } into { foo { bar : foobar }}
Name | Description |
ref |
FirebaseFirestore.DocumentReference<U>
The reference to the document. |
data |
UpdateMap
The field/value map to expand. The created DocumentSnapshot. |
Type | Description |
DocumentSnapshot<U> |
Name | Description |
U |
get(field)
get(field: string | FieldPath): any;
Retrieves the field specified by field
.
Name | Description |
field |
string | FieldPath
The field path (e.g. 'foo' or 'foo.bar') to a specific field. |
Type | Description |
any | {*} The data at the specified field location or undefined if no such field exists. |
isEqual(other)
isEqual(other: firestore.DocumentSnapshot<T>): boolean;
Returns true if the document's data and path in this DocumentSnapshot
is equal to the provided value.
Name | Description |
other |
FirebaseFirestore.DocumentSnapshot<T>
The value to compare against. {boolean} true if this |
Type | Description |
boolean |
protoField(field)
protoField(field: string | FieldPath): api.IValue | undefined;
Retrieves the field specified by 'fieldPath' in its Protobuf JS representation.
Name | Description |
field |
string | FieldPath
The path (e.g. 'foo' or 'foo.bar') to a specific field. |
Type | Description |
google.firestore.v1.IValue | undefined | The Protobuf-encoded data at the specified field location or undefined if no such field exists. |
toDocumentProto()
toDocumentProto(): api.IDocument;
Convert a document snapshot to the Firestore 'Document' proto.
Type | Description |
api.IDocument |
toWriteProto()
toWriteProto(): api.IWrite;
Convert a document snapshot to the Firestore 'Write' proto.
Type | Description |
api.IWrite |