Called by the Firestore SDK to convert a custom model object of type T
into a plain Javascript object (suitable for writing directly to the
Firestore database). To use set() with merge and mergeFields,
toFirestore() must be defined with Partial<T>.
[[["Easy to understand","easyToUnderstand","thumb-up"],["Solved my problem","solvedMyProblem","thumb-up"],["Other","otherUp","thumb-up"]],[["Missing the information I need","missingTheInformationINeed","thumb-down"],["Too complicated / too many steps","tooComplicatedTooManySteps","thumb-down"],["Out of date","outOfDate","thumb-down"],["Samples / code issue","samplesCodeIssue","thumb-down"],["Other","otherDown","thumb-down"]],["Last updated 2022-07-27 UTC."],[],[],null,["- [firebase](/docs/reference/js/v8/firebase).\n- [firestore](/docs/reference/js/v8/firebase.firestore).\n- FirestoreDataConverter\n\\\u003c T \\\u003e \nConverter used by `withConverter()` to transform user objects of type T\ninto Firestore data.\n\nUsing the converter allows you to specify generic type arguments when\nstoring and retrieving objects from Firestore.\n\nexample\n:\n\n class Post {\n constructor(readonly title: string, readonly author: string) {}\n\n toString(): string {\n return this.title + ', by ' + this.author;\n }\n }\n\n const postConverter = {\n toFirestore(post: Post): firebase.firestore.DocumentData {\n return {title: post.title, author: post.author};\n },\n fromFirestore(\n snapshot: firebase.firestore.QueryDocumentSnapshot,\n options: firebase.firestore.SnapshotOptions\n ): Post {\n const data = snapshot.data(options)!;\n return new Post(data.title, data.author);\n }\n };\n\n const postSnap = await firebase.firestore()\n .collection('posts')\n .withConverter(postConverter)\n .doc().get();\n const post = postSnap.data();\n if (post !== undefined) {\n post.title; // string\n post.toString(); // Should be defined\n post.someNonExistentProperty; // TS error\n }\n\n\nType parameters\n\n-\n\n T\n\nIndex\n\nMethods\n\n- [fromFirestore](/docs/reference/js/v8/firebase.firestore.FirestoreDataConverter#fromfirestore)\n- [toFirestore](/docs/reference/js/v8/firebase.firestore.FirestoreDataConverter#tofirestore)\n\nMethods\n\nfromFirestore\n\n- fromFirestore ( snapshot : [QueryDocumentSnapshot](/docs/reference/js/v8/firebase.firestore.QueryDocumentSnapshot) , options : [SnapshotOptions](/docs/reference/js/v8/firebase.firestore.SnapshotOptions) ) : T\n- Called by the Firestore SDK to convert Firestore data into an object of\n type T. You can access your data by calling: `snapshot.data(options)`.\n\n Parameters\n -\n\n snapshot: [QueryDocumentSnapshot](/docs/reference/js/v8/firebase.firestore.QueryDocumentSnapshot) \n A QueryDocumentSnapshot containing your data and metadata.\n -\n\n options: [SnapshotOptions](/docs/reference/js/v8/firebase.firestore.SnapshotOptions) \n The SnapshotOptions from the initial call to `data()`.\n\n Returns T\n\ntoFirestore\n\n- toFirestore ( modelObject : T ) : [DocumentData](/docs/reference/js/v8/firebase.firestore#documentdata)\n- Called by the Firestore SDK to convert a custom model object of type T\n into a plain Javascript object (suitable for writing directly to the\n Firestore database). To use `set()` with `merge` and `mergeFields`,\n `toFirestore()` must be defined with `Partial\u003cT\u003e`.\n\n Parameters\n -\n\n modelObject: T\n\n Returns [DocumentData](/docs/reference/js/v8/firebase.firestore#documentdata)\n- toFirestore ( modelObject : Partial \\\u003c T \\\u003e , options : [SetOptions](/docs/reference/js/v8/firebase.firestore.SetOptions) ) : [DocumentData](/docs/reference/js/v8/firebase.firestore#documentdata)\n-\n\n Parameters\n -\n\n modelObject: Partial\\\u003cT\\\u003e\n -\n\n options: [SetOptions](/docs/reference/js/v8/firebase.firestore.SetOptions)\n\nReturns [DocumentData](/docs/reference/js/v8/firebase.firestore#documentdata)"]]
Converter used by
withConverter()
to transform user objects of type T into Firestore data.Using the converter allows you to specify generic type arguments when storing and retrieving objects from Firestore.
class Post { constructor(readonly title: string, readonly author: string) {} toString(): string { return this.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)!; return new 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 }