I want to merge data (not overwrite) in my firebase db. Therefore I had a look at the following document: https://firebase.google.com/docs/firestore/manage-data/add-data
And I tried to do the same with my setDoc but it doesn't work. I always get an error (Uncaught TypeError: n.indexOf is not a function). As far as I understood, firebase tries to use my { merge: true } and put it into the path which for sure throws an error.
How can I merge data in firebase 9, I can't figure it out.
Here my source code:
setDoc(doc(db, "users", userStore.id, "profile_data", "user_profile", { merge: true }), {
country: selectedCountry.value,
year_born: selectedYearBorn.value,
gender: selectedGender.value,
profile_complete: true
}).then( () => {
console.log("success")
}).catch(() => {
console.log("alert")
})
I guess I places the { merge: true } option at the wrong place but where do I have to put it?
Btw: The connection to the database works perfectly fine, so there is no connection problem or anything.
Thanks!
CodePudding user response:
The options are to be passed to the setDoc
operation, but you're passing them to doc()
.
To solve this:
setDoc(doc(db, "users", userStore.id, "profile_data", "user_profile"), {
country: selectedCountry.value,
year_born: selectedYearBorn.value,
gender: selectedGender.value,
profile_complete: true
}, { merge: true })