iamshaunjp/node-crash-course

blog_details controller

Opened this issue · 0 comments

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
express@4.17.1