Reputation:
I am trying to remove a map form an array in Firestore by using Javascript Could any one help me with that?
By the way I used those several methods but none of them are working at all.
First method:
imgRef.update({
"images": firebase.firestore.FieldValue.arrayRemove({
"caption":...,
"id":...,
"postID":...,
"url":...,
"userID":...
})
});
Second method:
imgRef.update({
"images": firebase.firestore.FieldValue.arrayRemove(0)
});
Thanks for all your help
Upvotes: 4
Views: 1688
Reputation:
Here is the full code of recall and update ('remove') the element from array:
imgRef.get().then(function(doc) {
if (doc.exists) {
imgRef.update({
"images": firebase.firestore.FieldValue.arrayRemove(doc.data().images[position])
});
}
})
.catch(function(error) {
console.error(error.message);
});
Upvotes: 3
Reputation: 317477
Your first code sample will only work if you provide all of the map fields, and the exact values of the fields that match the array item to remove. Anything different, and it does nothing. Since you don't show the values, there's nothing we can do to say what might be going wrong.
If you don't know all of the exact values, your only alternative is the read the document, modify the array in the snapshot by finding and removing the array element that should be removed, and writing the new array back to the document.
Upvotes: 4