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
contentis 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);