Below at bottom, is POST request handler (function) which implements Delete feature for a book info and then re-directs to the link “./catalog/books” on the site .
But seems like it is not deleting the individual book object from database (mongoDB) and not re-directing back !
code:
exports.author_delete_post = asyncHandler(async (req, res, next) => {
// Get details of author and all their books (in parallel)
console.log("POST request to author_delete_post route");
const [author, allBooksByAuthor] = await Promise.all([
Author.findById(req.params.id).exec(),
Book.find({ author: req.params.id }, "title summary").exec(),
]);
// console.log(author);
if (allBooksByAuthor.length > 0) {
// Author has books. Render in same way as for GET route.
res.render("author_delete", {
title: "Delete Author",
author: author,
author_books: allBooksByAuthor,
// action: mongoDB,
});
return;
}
else {
// Author has no books. Delete object and redirect to the list of authors.
await Author.findByIdAndDelete(req.body.author._id);
res.redirect("/catalog/authors");
}
});