當我嘗試通過 setDoc() 更新時,我嘗試通過 react native expo 將新欄位添加到 firestore 中的現有資料中來更新資料,所有現有資料都已洗掉,并且僅更新一個 remian 如何將資料更新到 firebase 中的同一檔案中

有人可以指導我如何在不丟失 firestore 中現有資料的情況下添加新欄位。
這是我的 setDoc 嘗試
const updateUser = async(key, value) => {
await addDoc(doc(db,'users',user.uid), { [key]: value })
};
我嘗試添加是新的 Field 。如何添加新欄位我也嘗試如下
const updateUser = async(key,value)=>{
const updateRef = doc(db, "users",value);
await updateDoc(updateRef, {
[key]: true
});
}
這是我嘗試添加的新欄位

uj5u.com熱心網友回復:
您可以firestore從 firebase 庫匯入
import firestore from '@react-native-firebase/firestore';
然后你就可以得到你需要的檔案
const doc = firestore().collection('Users').doc('ABC')
之后,您可以按功能更新您的檔案 update()
firestore()
.collection('Users')
.doc('ABC')
.update({
age: 31,
})
.then(() => {
console.log('User updated!');
});
您可以在本檔案https://rnfirebase.io/firestore/usage 中找到有關 Firestore 使用的詳細資訊
uj5u.com熱心網友回復:
您需要設定選項合并。
火力基地 9
setDoc(doc(db, path, data.id), data, { merge: true})
火力基地 8-
db.collection("cities").doc(data.id).set(data, { merge: true })
在這兩個庫中,您都可以使用 functionupdateDoc()或 v8...doc().update()
轉載請註明出處,本文鏈接:https://www.uj5u.com/qianduan/371093.html
