I'm new to react native, I'm trying to delete a specific field but having issue
I'm trying to remove cm0IF5KopLgxnJTUtfi403kJuMl2 in the FriendList
Here is my code:
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()})
})
})
}
How do I delete the FriendsUID?
CodePudding user response:
Because you are working with a ID map instead of an array, you will need to change out your code to (omitted unrelated parts):
(doc) => {
const friendsListMap = doc.get('FriendsList');
delete friendsListMap[uidToDelete];
return doc.ref.update({ FriendsList: friendsListMap });
}
or
(doc) => {
return doc.ref.update({
[`FriendsList.${uidToDelete}`]: firebase.firestore.FieldValue.delete()
});
}
You should update your code to properly chain the Promises together (some are floating, use querySnapshot.docs.map
and Promise.all
) and consider making use of a transaction to perform these changes so you can ensure that data isn't wrongly being deleted.
CodePudding user response:
If you are trying to remove a single item from the FriendsList
array, you need the arrayRemove
operation. From the documentation on updating items in an array:
await washingtonRef.update({
regions: arrayRemove("east_coast")
});