Reputation: 151
How to delete object from array field by array index? or suggest another way!
i want to choose object from files array and delete!
Upvotes: 1
Views: 240
Reputation: 1065
There is no choose and delete
method from an array in cloud firestore. What you can do is get that array to a temporary array and remove the object that you need to delete from the array and overwrite the document array field
with your newly updated temporary array.
const deleteObjFromArr = async (index) => {
try {
/*
* Get the files array to a temporary array.
* If you have already gotten the document to an object,
* assign the object field to the temporary array
*/
const docRef = firebase.firestore().collection('tasks-collection').doc('tasks');
const tempArr = (await docRef.get()).files;
// Remove object from array
tempArr.splice(index, 1);
// Save new array in cloud firestore
await docRef.update({ files: tempArr });
console.log('Files array updated successfully');
} catch (err) {
console.log('Error occured. Operation terminated.');
console.log(err);
}
}
Upvotes: 1