1

I am building a simple app that displays an index of comments, allows posting of new comments, and editing+deleting of existing comments. I do this following Colt Steele's course section 371.

At a certain point, my code no longer recognizes the object I reference. Then it recognizes it again randomly. I am not sure where my problem is.

Index.js:

    const express = require('express')
    const path = require('path')
    const methodOverride = require('method-override')
    const app = express();
    const { v4: uuid } = require('uuid'); //For generating ID's
 
 
    app.set('view engine', 'ejs')
    app.set('views', path.join(__dirname, 'views'))
    app.use(express.urlencoded({ extended: true }))
    app.use(express.json())
    app.use(methodOverride('_method'))
 
 
 
    let comments = [
        {
            username: 'External_Mall_3897',
            comment: 'that Kana"s death stare when she sees Aqua princess carry Akane 😂',
            score: 564,
            id: uuid()
 
        },
        {
            username: 'Acrzyguy',
            comment: 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
            score: 304,
            id: uuid()
        },
        {
            username: 'mr_miscellaneous123',
            comment: 'How on earth did Aqua keep up that kind of acting for all the performances?',
            score: 461,
            id: uuid()
        },

    ]
 
    app.get('/comments', (req, res) => {
        res.render('index', { comments })
    })
 
    app.get('/comments/new', (req, res) => {
        res.render('new')
    })
    app.post('/comments', (req, res) => {
        const { username, comment } = req.body;
        comments.push({ username, comment, score: 0, id: uuid() })
        res.redirect('/comments')
    })
 
    app.get('/comments/:id', (req, res) => {
        const { id } = req.params;
        const comment = comments.find(c => c.id === id);
        res.render('show', { comment })
    })
    app.get('/comments/:id/edit', (req, res) => {
        const { id } = req.params;
        const comment = comments.find(c => c.id === id);
        res.render('edit', { comment })
    })
    app.patch('/comments/:id/', () => {
        const { id } = req.params;
        const foundComment = comments.find(c => c.id === id)
        const newCommentText = req.body.comment
        foundComment.comment = newCommentText;
        res.redirect('/comments')
        })
 
        app.listen(3030, () => {
        console.log('listening on 3030')
        })





index.ejs: https://pastebin.com/rLr4HHDT
 
new.ejs: https://pastebin.com/Dw1WRjx1
 
show.ejs: https://pastebin.com/9SeEKEjy
 
edit.ejs: https://pastebin.com/j0HpFLpJ

1 Answer 1

0

It looks like you're trying to reference a comment with an id that doesn't exist. You should probably check for that and explicitly handle it:

app.patch('/comments/:id/', () => {
    const { id } = req.params;
    const foundComment = comments.find(c => c.id === id)

    // If the comment isn't found, return a 404
    if (!foundComment)
        res.status(404).end(`No comment found with id=${id}`)
        return
    }

    // If the comment is found, update it
    const newCommentText = req.body.comment
    foundComment.comment = newCommentText;
    res.redirect('/comments')
})
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.