繁体   English   中英

我正在尝试从 Firestore 中删除一些数据,但出现问题(反应)

[英]I am trying to delete some data from firestore but there is a problem (react)

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 是 arrays 的 object 现在运行此代码时出现此错误

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,您可以在每次调用 function 时处理它。

编辑:如果您在paymentRefs中的每个 object 中有付款的 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',
        }
      }
    })
  }
}

我是怎么做到的

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM