DocumentSnapshot

DocumentSnapshot

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.

Constructor

new DocumentSnapshot(ref, fieldsProto, readTime, createTime, updateTime)

Parameters:
Name Type Description
ref

The reference to the document.

fieldsProto

The fields of the Firestore Document Protobuf backing this document (or undefined if the document does not exist).

readTime

The time when this snapshot was read (or undefined if the document exists only locally).

createTime

The time when the document was created (or undefined if the document does not exist).

updateTime

The time when the document was last updated (or undefined if the document does not exist).

Source:

Members

(readonly) createTime :Timestamp|undefined

The time the document was created. Undefined for documents that don't exist.

Source:
Example
let documentRef = firestore.doc('col/doc');

documentRef.get().then(documentSnapshot => {
  if (documentSnapshot.exists) {
    let createTime = documentSnapshot.createTime;
    console.log(`Document created at '${createTime.toDate()}'`);
  }
});

(readonly) exists :boolean

True if the document exists.

Source:
Example
let documentRef = firestore.doc('col/doc');

documentRef.get().then((documentSnapshot) => {
  if (documentSnapshot.exists) {
    console.log(`Data: ${JSON.stringify(documentSnapshot.data())}`);
  }
});

(readonly) id :string

The ID of the document for which this DocumentSnapshot contains data.

Source:
Example
let documentRef = firestore.doc('col/doc');

documentRef.get().then((documentSnapshot) => {
  if (documentSnapshot.exists) {
    console.log(`Document found with name '${documentSnapshot.id}'`);
  }
});

(readonly) readTime :Timestamp

The time this snapshot was read.

Source:
Example
let documentRef = firestore.doc('col/doc');

documentRef.get().then(documentSnapshot => {
  let readTime = documentSnapshot.readTime;
  console.log(`Document read at '${readTime.toDate()}'`);
});

(readonly) ref :DocumentReference

A DocumentReference for the document stored in this snapshot.

Source:
Example
let documentRef = firestore.doc('col/doc');

documentRef.get().then((documentSnapshot) => {
  if (documentSnapshot.exists) {
    console.log(`Found document at '${documentSnapshot.ref.path}'`);
  }
});

(readonly) updateTime :Timestamp|undefined

The time the document was last updated (at the time the snapshot was generated). Undefined for documents that don't exist.

Source:
Example
let documentRef = firestore.doc('col/doc');

documentRef.get().then(documentSnapshot => {
  if (documentSnapshot.exists) {
    let updateTime = documentSnapshot.updateTime;
    console.log(`Document updated at '${updateTime.toDate()}'`);
  }
});

Methods

data() → {DocumentData|undefined}

Retrieves all fields in the document as an object. Returns 'undefined' if the document doesn't exist.

Source:
Example
let documentRef = firestore.doc('col/doc');

documentRef.get().then(documentSnapshot => {
  let data = documentSnapshot.data();
  console.log(`Retrieved data: ${JSON.stringify(data)}`);
});

get(field) → {*}

Retrieves the field specified by field.

Parameters:
Name Type Description
field string | FieldPath

The field path (e.g. 'foo' or 'foo.bar') to a specific field.

Source:
Example
let documentRef = firestore.doc('col/doc');

documentRef.set({ a: { b: 'c' }}).then(() => {
  return documentRef.get();
}).then(documentSnapshot => {
  let field = documentSnapshot.get('a.b');
  console.log(`Retrieved field value: ${field}`);
});

isEqual(other) → {boolean}

Returns true if the document's data and path in this DocumentSnapshot is equal to the provided value.

Parameters:
Name Type Description
other *

The value to compare against.

Source: