0

I am creating an application using Nodejs and React, but the comment function does not work. I do not know the cause.

Error :

board validation failed: comments.0.content: Path content is required.

I don't know why this is not working. What mistake did I make?

route/api/board.js

router.post( '/:id/comments', [ auth, [ check('content', 'input your content. ') .not() .isEmpty() ] ], async (req, res) => { const errors = validationResult(req); if (!errors.isEmpty()) { return res.status(400).json({ errors: errors.array() }, 'isEmpty'); } try { const user = await User.findById(req.user.id).select('-password'); const board = await Board.findById(req.params.id); const newComment = new Board({ content: req.body.content, user: req.user.id }); board.comments.unshift(newComment); await board.save(); res.json(board.comments); } catch (err) { console.error(err.message); res.status(500).send('Server error!!'); } } ); 

models/Board.js

const mongoose = require('mongoose'); const Schema = mongoose.Schema; const BoardSchema = new Schema({ user: { type: Schema.Types.ObjectId, ref: 'user' }, title: { type: String, required: true }, description: { type: String, required: true }, view_count: { type: Number, default: 1 }, created_at: { type: Date, default: Date.now }, likes: [ { user: { type: Schema.Types.ObjectId, ref: 'user' } } ], comments: [ { user: { type: Schema.Types.ObjectId, ref: 'user' }, content: { type: String, required: true }, created_at: { type: Date, default: Date.now } } ] }); module.exports = Board = mongoose.model('board', BoardSchema); 

1 Answer 1

1

The error is pretty explicit, one of your comments in the comments array doesn't have a property content which seem to be required based on your model.

So double check what you are sending to the server by debugging/logging the content of the req.body.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.