【发布时间】:2021-06-06 19:35:06
【问题描述】:
【问题讨论】:
标签: reactjs firebase google-cloud-firestore
【问题讨论】:
标签: reactjs firebase google-cloud-firestore
云 Firestore 中的数组中没有 choose and delete 方法。您可以做的是将该数组放入一个临时数组,然后从数组中删除您需要删除的对象,并用您新更新的临时数组覆盖文档array field。
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);
}
}
【讨论】:
filter 方法过滤掉没有该查找键的数组。 tempArr = tempArr.filter(e => e.attributeName != findingKeyword)