Stay organized with collections
Save and categorize content based on your preferences.
Except as otherwise noted, the content of this page is licensed under the Creative Commons Attribution 4.0 License, and code samples are licensed under the Apache 2.0 License. For details, see the Google Developers Site Policies. Java is a registered trademark of Oracle and/or its affiliates.
Last updated 2022-07-27 UTC.
[null,null,["Last updated 2022-07-27 UTC."],[],[],null,["- [firebase](/docs/reference/node/firebase).\n- [firestore](/docs/reference/node/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/node/firebase.firestore.FirestoreDataConverter#fromfirestore)\n- [toFirestore](/docs/reference/node/firebase.firestore.FirestoreDataConverter#tofirestore)\n\nMethods\n\nfromFirestore\n\n- fromFirestore ( snapshot : [QueryDocumentSnapshot](/docs/reference/node/firebase.firestore.QueryDocumentSnapshot) , options : [SnapshotOptions](/docs/reference/node/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/node/firebase.firestore.QueryDocumentSnapshot) \n A QueryDocumentSnapshot containing your data and metadata.\n -\n\n options: [SnapshotOptions](/docs/reference/node/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/node/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/node/firebase.firestore#documentdata)\n- toFirestore ( modelObject : Partial \\\u003c T \\\u003e , options : [SetOptions](/docs/reference/node/firebase.firestore.SetOptions) ) : [DocumentData](/docs/reference/node/firebase.firestore#documentdata)\n-\n\n Parameters\n -\n\n modelObject: Partial\\\u003cT\\\u003e\n -\n\n options: [SetOptions](/docs/reference/node/firebase.firestore.SetOptions)\n\nReturns [DocumentData](/docs/reference/node/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 }