reactjs - firebase 按索引从字段数组中删除对象
解决方案
choose and delete
Cloud Firestore 中的数组没有方法。您可以做的是将该数组放入临时数组,然后从数组中删除您需要删除的对象,并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);
}
}
推荐阅读
- spring-boot - Docker 多阶段构建未运行所有阶段
- php - 如何在 FPDF 上添加自定义纸张尺寸?
- go - 在 Goroutine 中更新值
- reactjs - 如何使用已在自定义挂钩中更新的状态更新父组件?
- c - 尝试从 C 中的结构将多行写入文件
- python - os.getenv() 总是返回 None
- python - 显示 -h 时的 argparse 自定义“可选参数描述”
- node.js - 如何从节点 API 访问私有 Git 存储库中的 JSON 文件
- arrays - 如何在 Angular 中使用平面数组创建树
- python - 尝试在 Heroku 上部署烧瓶应用程序,部署工作正常,直到我在我的代码中添加会话,然后它给出 500 内部错误。如何解决这个问题?