Reputation: 51
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 is an object of arrays now when running this code i get this error
Unhandled Rejection (FirebaseError): Expected type 't', but it was: a custom Object object
Upvotes: 1
Views: 160
Reputation: 51
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',
}
}
})
}
}
that how I did it
Upvotes: 0
Reputation: 470
Try this instead:
export function DeletePayments(paymentRefs) {
const batch = firebase.firestore().batch();
paymentRefs.forEach((ref) => {
batch.delete(ref);
});
return batch.commit()
}
This will delete all document references at the end of the batch and will return a promise that you can handle every time the function is called.
EDIT:
If you have the id of the payment inside the every object in paymentRefs
, you could try something like this.
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()
}
Upvotes: 2
Reputation: 4581
Find an example for how to clean some notification collection in case its useful...
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();
}
})
}
Upvotes: 0