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

blog_details controller #23

Open
Jogala opened this issue Jul 21, 2021 · 0 comments
Open

blog_details controller #23

Jogala opened this issue Jul 21, 2021 · 0 comments

Comments

@Jogala
Copy link

Jogala commented Jul 21, 2021

Hi Net Ninja,

I followed your tutorial on youtube (thanks again for the great tutorial ), and I had to adapt the blog_details controller at two points for not running into errors (see (1) and (2) in the code snippet below):

const blog_details = (req, res) => {

    //(1) id contains a space as first character, I don't know why, I have to remove it
    const id = req.params.id.slice(1)

    Blog.findById(id).then(result => {

        //(2) if blog is not found result = null, I have to check for that for preventing error at this point,
        // otherwise the webpage hangs
        // you can trigger this behaviour by using a valid blog id and then change e.g. the last digit
        if(result != null)
            res.render('./blogs/details', { blog: result, title: 'Blog Details' })
        else
            res.status(404).render('404', {title: 'Blog not found' })

    }).catch( err => {
        console.log(err)
        // you can trigger this behavior e.g. with /blogs/isajfjklsfnhhsafkhnbsdf
        res.status(404).render('404', {title: 'Site not found' })
    })
}

I am using:
wsl2 Ubuntu 20.04.2 LTS
node v14.17.2
[email protected]

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