I try to update data by adding new Field into existing data in firestore by react native expo when I try to update by setDoc() , all existing data removed and only newly update one remian How can update data into same document in firebase
can someone guide me how can add new Field without losing existing data in firestore.
here is my attemp by setDoc
const updateUser = async(key, value) => {
await addDoc(doc(db,'users',user.uid), { [key]: value })
};
I try to add is new Field . how can add new field I also try as follow
const updateUser = async(key,value)=>{
const updateRef = doc(db, "users",value);
await updateDoc(updateRef, {
[key]: true
});
}
this is new Field I try to add
I try to update data by adding new Field into existing data in firestore by react native expo when I try to update by setDoc() , all existing data removed and only newly update one remian How can update data into same document in firebase
can someone guide me how can add new Field without losing existing data in firestore.
here is my attemp by setDoc
const updateUser = async(key, value) => {
await addDoc(doc(db,'users',user.uid), { [key]: value })
};
I try to add is new Field . how can add new field I also try as follow
const updateUser = async(key,value)=>{
const updateRef = doc(db, "users",value);
await updateDoc(updateRef, {
[key]: true
});
}
this is new Field I try to add
Share Improve this question edited Dec 1, 2021 at 23:01 Frank van Puffelen 601k85 gold badges890 silver badges860 bronze badges Recognized by Google Cloud Collective asked Dec 1, 2021 at 20:01 user17466327user17466327 6-
1
From stackoverflow./questions/70144432/…: "When you call
addDoc
Firestore generates the ID for the new document for you. If you want to specify the ID yourself, usesetDoc
instead." – Frank van Puffelen Commented Dec 1, 2021 at 20:23 -
@Mises: did you mean to change the call from
addDoc
tosetDoc
in the code here? That sounds like a change that OP themselves should make. – Frank van Puffelen Commented Dec 1, 2021 at 20:58 - @FrankvanPuffelen I wrote a summary what i changed so hope he get know that he was using wrong function in code. – Mises Commented Dec 1, 2021 at 21:02
- But that should be an answer, not an edit to the question. – Frank van Puffelen Commented Dec 1, 2021 at 21:26
-
@FrankvanPuffelen He was writing about
setDoc()
func and only in code snippet he hadaddDoc()
. If he had an option { merge: true } in code i would point a mistake that he was using wrong func. – Mises Commented Dec 1, 2021 at 22:56
2 Answers
Reset to default 4You can import firestore
from firebase library
import firestore from '@react-native-firebase/firestore';
And then you can get the document reference you need
const docRef = firestore().collection('Users').doc('ABC')
after that you can update your doc by method update()
docRef
.update({
age: 31,
})
.then(() => {
console.log('User updated!');
});
Detailed about usage of firestore you can find in this documentation https://rnfirebase.io/firestore/usage
You need to set option merge.
firebase 9+
function updateData(collPath, data) {
setDoc(doc(db, collPath, data.id), data, { merge: true})
}
firebase 8-
function updateData(collPath, data) {
db.collection(collPath).doc(data.id).set(data, { merge: true })
}
And in both of libraries you can use function updateDoc()
or v8 ...doc().update()