I've encountered an issue while utilizing the FirestoreDataConverter
for transforming objects into firestore data. It appears that the converter functions properly with the addDoc
and setDoc
operations, but fails to trigger the toFirestore
function when used in conjunction with the updateDoc
operation.
Example for Reproduction
interface Article {
title: string;
content: string;
}
async function addAndUpdateArticle() {
const colRef = collection(firestore, 'articles')
.withConverter(articleConverter);
const article: Article = { title: 'Title', content: 'Text'};
const docRef = doc(colRef);
await setDoc(docRef, article); // Triggers toFirestore function
const addedArticle = (await getDoc(docRef)).data(); // Triggers fromFirestore function
if (!addedArticle) return;
await updateDoc(docRef, addedArticle) // Does NOT trigger toFirestore function
}
const articleConverter: FirestoreDataConverter<Article> = {
toFirestore(article: Article): DocumentData {
console.log("Conversion happened in to-converter");
return { ...article};
},
fromFirestore(docSnap: QueryDocumentSnapshot): Article {
console.log("Conversion happened in from-converter")
return docSnap.data() as Article;
},
};
Expected Output in Console
Conversion happened in to-converter
Conversion happened in from-converter
Conversion happened in to-converter
Actual Output in Console
Conversion happened in to-converter
Conversion happened in from-converter
This issue specifically occurs with the use of updateDoc
. When switching to setDoc
, the functionality works properly. Could it be that updateDoc
is not supported by the Converter? There doesn't seem to be any mention of this in the documentation.
Link to Documentation