我正试图从firestore中删除一些数据,但出现了问题(反应)


export function DeletePayment(paymentRefs) {
return async (dispatch) => {
await firebase
.firestore()
.collection("payments")
.doc(firebase.auth().currentUser.uid)
.collection("payment")
.where("uid", "==", firebase.auth().currentUser.uid)
.onSnapshot(async (result) => {
const batch = firebase.firestore().batch();
paymentRefs.forEach((ref) => {
batch.delete(ref);
});
await batch.commit();
})
}
}

paymentRefs现在是数组的一个对象,当运行此代码时,我得到了这个错误

Unhandled Rejection (FirebaseError): Expected type 't', but it was: a custom Object object

试试这个:

export function DeletePayments(paymentRefs) {
const batch = firebase.firestore().batch();
paymentRefs.forEach((ref) => {
batch.delete(ref);
});
return batch.commit()
}

这将在批处理结束时删除所有文档引用,并在每次调用函数时返回一个可以处理的promise。

编辑:如果你在paymentRefs中的每个对象中都有付款的id,你可以尝试这样的方法。

export function DeletePayments(paymentRefs) {
const batch = firebase.firestore().batch();
paymentRefs.forEach((ref) => {
batch.delete(firebase.firestore().doc(`payments/${firebase.auth().currentUser.uid}/payment/${ref.id}`));
});
return batch.commit()
}

找到一个如何清理某些通知集合的示例,以防其有用。。。

async function checkForNotifClean(docsToKeep:firebase.firestore.QuerySnapshot<firebase.firestore.DocumentData>) {
const db = firebase.firestore();
const notifCollection = db.collection('notifications')
const allData = await notifCollection.get();
allData.docs.forEach(doc => {
const filtered = docsToKeep.docs.filter(entry => entry.id === doc.id); 
const needsErase = filtered.length === 0;
if (needsErase) {
const id = doc.id; 
notifCollection.doc(id).delete();
}
})
}
export function DeletePayment(paymentRefs) {
return async (dispatch) => {
const batch = firebase.firestore().collection("payments").doc(firebase.auth().currentUser.uid).collection("payment");
paymentRefs.forEach(async (ref) => {
await batch.doc(ref).delete();
});
dispatch({
type: 'ENQUEUE_SNACKBAR', notification: {
message: 'Payment has been Deleted',
key: new Date().getTime() + Math.random(),
options: {
variant: 'success',
}
}
})
}
}

我是怎么做到的

最新更新