I have a problem with deleting an Object
out of an Array
in firestore
.
I have this data in firestore:
And now I would like to delete e.g the second Object
out of the posts Array
.
Code:
JavaScript
x
11
11
1
deletePic () {
2
let docId = `${this.currentUser.uid}`
3
4
fb.usersCollection.doc(docId).update({
5
posts: firebase.firestore.FieldValue.arrayRemove()
6
})
7
.catch(function(error) {
8
console.error("Error removing document: ", error);
9
});
10
}
11
But I do not know how to define arrayRemove()
These are the pictures and each one has a delete button to delete the picture.
Advertisement
Answer
EDIT: Warning Do not use my solution as it is more than 3 years old, nowadays there is new solution : https://stackoverflow.com/a/59745086/6668441 Mine was a pure js one and is a bad pattern.
Can’t you use filter ? And then return the new posts array to your fb.usersCollection
method
JavaScript
1
3
1
//deleteId is the id from the post you want to delete
2
posts.filter(post => post.id !== deleteId);
3
edit : So This should be something like :
JavaScript
1
13
13
1
deletePic (deleteId) {
2
let docId = `${this.currentUser.uid}`
3
4
//deleteId is the id from the post you want to delete
5
6
fb.usersCollection.doc(docId).update({
7
posts: posts.filter(post => post.id !== deleteId);
8
})
9
.catch(function(error) {
10
console.error("Error removing document: ", error);
11
});
12
}
13