我是本機反應的新手,我正在嘗試洗掉特定欄位但有問題

我正在嘗試洗掉 FriendList 中的 cm0IF5KopLgxnJTUtfi403kJuMl2
這是我的代碼:
export default function deletefriends(FriendsUID){
const friendremove = async() =>{
// get current users uid
const Uid = firebase.auth().currentUser.uid
// define the current users
const currentUID = firebase.firestore().collection('users').where(
"UID", "==", firebase.auth().currentUser.uid).get();
// add the other users uid to current users friendlist
currentUID.then((querySnapshot) =>
{querySnapshot.forEach((doc) => {doc.ref.update(
{FriendsList:FriendsUID.delete()})
})
})
}
如何洗掉 FriendsUID?
uj5u.com熱心網友回復:
因為您使用的是 ID 映射而不是陣列,所以您需要將代碼更改為(省略無關部分):
(doc) => {
const friendsListMap = doc.get('FriendsList');
delete friendsListMap[uidToDelete];
return doc.ref.update({ FriendsList: friendsListMap });
}
或者
(doc) => {
return doc.ref.update({
[`FriendsList.${uidToDelete}`]: firebase.firestore.FieldValue.delete()
});
}
您應該更新您的代碼以正確地將 Promise 鏈接在一起(有些是浮動的,使用querySnapshot.docs.map和Promise.all)并考慮使用事務來執行這些更改,這樣您就可以確保資料不會被錯誤地洗掉。
uj5u.com熱心網友回復:
如果您嘗試從FriendsList陣列中洗掉單個專案,則需要該arrayRemove操作。從更新陣列中的專案的檔案中:
await washingtonRef.update({
regions: arrayRemove("east_coast")
});
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/353765.html
標籤:javascript 火力基地 反应原生 谷歌云firestore
上一篇:為什么要使用函式借用
