iamshaunjp/node-crash-course

Blogs with the same id lenght throwing ejs error. [With solution]

Opened this issue · 1 comments

blog_details controller returns "null" if he couldn't find a blog in the database.

Simple solution:
Add "if (null === result) throw err;" to blogController.js on line 17.

I just ran into the same issue and fixed it by rewriting blog_details like the following. This was a very confusing problem when trying to learn this material, and it happens if you simply try to access the URL of a deleted blog entry.

const blog_details = (req, res) => {
  const id = req.params.id;
  Blog.findById(id)
    .then((result) => {
      if (result === null) {
        console.log(result);
        throw err;
      } else {
        res.render('details', { blog: result, title: 'Blog Details' });
      }
    })
    .catch((err) => {
      console.log(err);
      res.render('404', { title: 'Blog not found' });
    });
};