我正在使用Firebase开发React应用。我想更新所有集合数据,包括其子集合。这意味着我必须擦除所有子集合中的所有数据并插入更新的数据。所以我创建了以下代码:
async update(editingPlan) {
return new Promise(async (resolve) => {
const planRef = database.collection(PLAN_COLLETION)
.doc(editingPlan.id);
console.log(editingPlan);
await keys(editingPlan.days).forEach(async day => {
const docs = await planRef.collection(day).get();
docs.forEach(async doc => {
await doc.ref.delete();
});
});
await forIn(editingPlan.days, (activities, day) => {
const listRef = planRef.collection(day);
activities.forEach(async (activity, index) => {
const { id, ...data } = activity;
if (id) {
await listRef.doc(id).update(data);
} else {
const doc = await listRef.add(data);
editingPlan.days[ day ][ index ] = { id: doc.id, ...data };
}
});
});
resolve(this._coercePlan(editingPlan));
});
}
该代码可以插入和更新数据,但是在执行此操作之前不会擦除数据。有什么建议吗?