Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

JavaScript Next Steps

Jakub Drobina
Jakub Drobina
12,675 Points

Trying to add comments to articles

Im trying to add comments to questions but im stuck and I dont know what i did wrong. After run program i see this error: TypeError: C:\Users\drobi\Desktop\projekty\SQLnode2\views\articles\show.pug:15 13| div 14| label(for="title") Title:

15| input(type="text", name="title" value=comment.title) 16| div 17| label(for="body") Body: 18| textarea(name="body")= comment.body

Cannot read property 'title' of undefined - I know what that error means but i dont know where my mistake is.

I created new model comment.js Theres a code: 'use strict'; const Sequelize = require('sequelize'); const moment = require('moment');

module.exports = (sequelize) => { class Comment extends Sequelize.Model { publishedAt() { const date = moment(this.createdAt).format("MMMM D, YYYY, h:mma"); return date; } } Comment.init({ title: { type: Sequelize.STRING, validate: { notEmpty: { msg: '"Title" is required' } } }, body: Sequelize.TEXT }, { sequelize });

return Comment; };

added new route comments.js:

const express = require('express'); const router = express.Router(); const Comment = require('../models').Comment;

function asyncHandler(cb){ return async(req, res, next) => { try { await cb(req, res, next) } catch(error){ res.status(500).send(error); } } }

/* GET comments listing. */ router.get('/:id', asyncHandler(async (req, res) => { const comments = await Comment.findAll({ order: [["createdAt", "DESC"]] }); res.render("articles/show", { comments, title: "Sequelize-It!" }); }));

/* Create a new comment form. */

router.get('/:id', (req, res) => { res.render("articles/show", { comment: {}, title: "New comment" }); });

// Post created Comment router.post('/:id', asyncHandler(async (req, res) => { let comment; try { comment = await Comment.create(req.body); } catch (error) { if(error.name === "SequelizeValidationError") { // checking the error comment = await Comment.build(req.body); res.render("articles/show", { comment, errors: error.errors, title: "New Comment" }) } else { throw error; // error caught in the asyncHandler's catch block } } }));

module.exports = router;

and added few lines of code to show.pug: extends ../layout

block content h2= article.title include by_line p= article.body a(href="../") ← Back | | a(href="/articles/" + article.id + "/edit") Edit Article | | a(href="/articles/" + article.id + "/delete") Delete Article form(action="/:id", method="post") div label(for="title") Title: input(type="text", name="title" value=comment.title) div label(for="body") Body: textarea(name="body")= comment.body input(type="submit")

Can someone please help me i cant solve this problem and i really would like to know wherem by mistake is. Thanks :D

1 Answer

please post your code using

```
code
```

to make it readable

anyway, the problem is likely caused by this line:

block content h2= article.title include by_line p= article.body a(href="../") ← Back | | a(href="/articles/" + article.id + "/edit")

Specifically, article.title.

I think it may work if you write simply title instead of article.title