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
Methods
fromFirestore
- from
Firestore(snapshot: QueryDocumentSnapshot, options: SnapshotOptions):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
snapshot:QueryDocumentSnapshot
A QueryDocumentSnapshot containing your data and metadata.
options:SnapshotOptions
The SnapshotOptions from the initial call to
data().
ReturnsT
toFirestore
- to
Firestore(modelObject: T):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 use
set()withmergeandmergeFields,toFirestore()must be defined withPartial<T>.Parameters
modelObject:T
ReturnsDocumentData
- to
Firestore(modelObject: Partial<T>, options: SetOptions):DocumentData Parameters
modelObject:Partial<T>
options:SetOptions
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.