So I have 2 collections in firestore,
- Users (collection)
- Inspection (collection)
Users have only documents named as different email addresses Inspection have documents named as different email addresses and then each document have another collection named "assignedToMe" and then this collection have different documents named as some unique ids.
What I want to do is whenever I delete a user, I want its document named as his email address to be deleted as well from Inspection(collection).
The issue I'm having here is I am able to delete the user from Users(collection) but unable to delete the document in Inspection(collection) for that particular user, here's my code and a structure of firestore.
router.use("/delete", [auth, admin], async function (req, res, next) {
const { email } = req.body;
const userRef = db.collection("Users").doc(email);
const insRef = db.collection("Inspection").doc(email);
const user = await userRef.get();
const inspection = await insRef.get();
if (!user.exists) {
return res.status(404).send("This user has already been deleted!");
} else {
const result = userRef.delete();
if (!inspection.empty) {
const myres = insRef.delete();
}
result && res.status(200).send("User deleted successfully.");
}
});

