Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
ghost opened this issue Aug 28, 2020 · 1 comment
Open

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

ghost opened this issue Aug 28, 2020 · 1 comment

Comments

@ghost
Copy link

ghost commented Aug 28, 2020

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.

@BryanWall
Copy link

BryanWall commented Aug 31, 2020

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' });
    });
};

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant