I am wondering if anyone has any suggestion on how to get around this error i am getting. I am using params to dynamically create links, however when i post a new blog post i am getting a casting error since it tries to create a new link before the blog has generated an ID. My code is as follows
router.get("/blog/:id", ensureAuthenticated, (req, res) => {
let id = req.params.id;
console.log(`id is ${id}`)
Blog.findOne({_id: id})
.then((result) => {
console.log(`result: ${result}`)
res.render("viewPost", {
post: result,
user: req.user,
});
})
.catch((error) => {
console.log(error)
})
});
router.post("/blog-new", (req, res) => {
const blog = new Blog({
title: req.body.title,
snippet: req.body.snippet,
body: req.body.blog,
})
blog.save()
.then((result) => {
res.redirect("blog/posted");
})
.catch((error) => {
console.log(error);
})
})
"blog/posted"
route – Alghazali505