I am trying to batch delete a document of a subcollection.
There are times when I might need to delete the same product document ID and different history document ID or multiple product document IDs and different history document IDs.
I am passing the product document ID and history document ID, and I already see them in the console. However, I receive this error once I try to batch.delete()
this:
This is what the docID and the historyId shows in the console:
import {
doc,
getDoc,
updateDoc,
deleteField,
collection,
getDocs,
addDoc,
increment,
} from "firebase/firestore";
import { db } from "../../Firebase/utilities";
async function deleteDocumentSubcollection(docID, historyId) {
try {
const batch = writeBatch(db);
//I think this is where the error appears
historyId.forEach((id) => {
batch.delete(doc(db, "products", docID, "history", historyId));
});
await batch.commit();
alert("deleted");
console.log("deleted");
} catch (err) {
console.log(err);
}
}
package.json
As stated in the documentation, the docId and historyId should corresponds to the specific Ids of documents. You can try to delete specific documents using their corresponding IDs, and just a note, IDs should not be array:
historyId.forEach((id) => {
batch.delete(doc(db, "products", "documentId", "history", id));
});