Closed mrleblanc101 closed 4 months ago
It works if I use .withConverter
instead.
Exemple:
const addDefaultFields = {
toFirestore(docData: DocumentData): DocumentData {
console.log('test 1');
return {
created_at: serverTimestamp(),
...docData,
}
},
fromFirestore(snapshot: QueryDocumentSnapshot, options: SnapshotOptions): DocumentData {
console.log('test 2');
const data = firestoreDefaultConverter.fromFirestore(snapshot)!;
return data;
},
};
...
async function addProject(option: Project) {
const project = await addDoc(collection(db, 'projects').withConverter(addDefaultFields), {
name: option.name,
user: user.value!.uid,
})
return { id: project.id, name: option.name };
}
Reproduction
.
Steps to reproduce the bug
I created this global converter, the fromFirestore gets called, but not the toFirestore. I see
test 2
in the console, but nevertest 1
, and the data in my firestore does not have the fieldcreated_at
.Expected behavior
Both toFirestore and fromFirestore should be called.
Actual behavior
Only the fromFirestore is called.
Additional information
I want to automatically add a
created_at
field so that I can sort by date when fetching.