message  Send Feedback

DocumentSnapshot

A DocumentSnapshot contains data read from a document in your Firestore database. The data can be extracted with .data() or .get() 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.

Properties

Methods


Properties

exists

exists: boolean;

Property of the DocumentSnapshot that signals whether or not the data exists. True if the document exists.


id

id: string;

Property of the DocumentSnapshot that provides the document's ID.


metadata

metadata: SnapshotMetadata;

Metadata about the DocumentSnapshot, including information about its source and local modifications.


ref

ref: DocumentReference;

The DocumentReference for the document included in the DocumentSnapshot.


Methods

data

data(): { [key: string]: value } | undefined;

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

Example

const user = await firebase.firestore().doc('users/alovelace').get();
console.log('User', user.data());

get

get(
  fieldPath: string | FieldPath
): any;

Retrieves the field specified by fieldPath. Returns undefined if the document or field doesn't exist.

Example

const user = await firebase.firestore().doc('users/alovelace').get();
console.log('Address ZIP Code', user.get('address.zip'));

Parameters

  • fieldPath: string | FieldPath
    The path (e.g. 'foo' or 'foo.bar') to a specific field.

isEqual

isEqual(
  other: DocumentSnapshot
): boolean;

Returns true if this DocumentSnapshot is equal to the provided one.

Example

const user1 = await firebase.firestore().doc('users/alovelace').get();
const user2 = await firebase.firestore().doc('users/dsmith').get();
// false
user1.isEqual(user2);

Parameters


React Native Market

Looking for pre-built React Native apps to kick start your next project? Check out the React Native Market by Invertase.

Your purchases help support our open-source projects such as React Native Firebase. All items are currently 50% off to celebrate the release of React Native Firebase version 6.
shopping_cartVisit React Native Market