Converter used bywithConverter() to transform user objects of type Tinto Firestore data.

Using the converter allows you to specify generic type arguments whenstoring and retrieving objects from Firestore.

example
class Post {constructor(readonly title:string, readonly author:string) {}  toString():string {returnthis.title +', by ' +this.author;  }}const postConverter = {  toFirestore(post: Post): firebase.firestore.DocumentData {return {title: post.title, author: post.author};  },  fromFirestore(    snapshot: firebase.firestore.QueryDocumentSnapshot,    options: firebase.firestore.SnapshotOptions  ): Post {const data = snapshot.data(options)!;returnnew Post(data.title, data.author);  }};const postSnap =await firebase.firestore()  .collection('posts')  .withConverter(postConverter)  .doc().get();const post = postSnap.data();if (post !==undefined) {  post.title;// string  post.toString();// Should be defined  post.someNonExistentProperty;// TS error}

Type parameters

  • T

Index

Methods

fromFirestore

  • fromFirestore(snapshotQueryDocumentSnapshot, optionsSnapshotOptions):T
  • Called by the Firestore SDK to convert Firestore data into an object oftype T. You can access your data by calling:snapshot.data(options).

    Parameters

    ReturnsT

toFirestore

  • toFirestore(modelObjectT):DocumentData
  • Called by the Firestore SDK to convert a custom model object of type Tinto a plain Javascript object (suitable for writing directly to theFirestore database). To useset() withmerge andmergeFields,toFirestore() must be defined withPartial<T>.

    Parameters

    • modelObject:T

    ReturnsDocumentData

  • toFirestore(modelObjectPartial<T>, optionsSetOptions):DocumentData
  • Parameters

    ReturnsDocumentData

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 2022-07-27 UTC.