Routes not return the json, and return error Nodejs

I'm creating an api rest and I'm not able to access some routes, such as this GET: localhost: 3033 / classes / comments

https://gitlab.com/TBussola/teste-pratico-tindin

what am I doing wrong? I have another route file that works perfectly. Here is the file where I configure the use of the routes

const bodyParser = require('body-parser')
const classes = require('./classesRoute')
const comments = require('./commentsRoute')

module.exports = app => {
    app.use(bodyParser.json())
    app.use(classes)
    app.use(comments)
    app.get('/', (req, res) => res.send("Olá Teste"))
}

Here is the classesRoute file, all routes listed here work perfectly

const { Router } = require('express')
const ClasseController = require('../controllers/ClasseController')


const router = Router()

router.post('/classes', ClasseController.postClass)
router.get('/classes', ClasseController.getAllClass)
router.get('/classes/:_id', ClasseController.getClassById)
router.delete('/classes/:_id', ClasseController.deleteClass)
router.put('/classes/:_id', ClasseController.updateClass)


module.exports = router

Here is the commentsRoute, in this file the rotaPost works, the route to fetch a comment by the class id works, but it is not returning me a json, and it should. We also have here in this file the getAll that is returning me error

const { Router } = require('express')
const CommentController = require('../controllers/CommentController')

const router = Router()

router.post('/classes/comments', CommentController.postComment)
router.get('/classes/:_id/comments', CommentController.getCommentsByClass)
router.get('/classes/comments', CommentController.getAllComments)


module.exports = router

error returned on route getAll:

{
  "message": {
    "stringValue": "\"comments\"",
    "kind": "ObjectId",
    "value": "comments",
    "path": "_id",
    "reason": {}
  }
}

Here is the example of the Comment controller I’m using

const Comment = require('../models/Comment') const Classe = require('../models/Classe')

class CommentController {

    static async postComment(req, res) {
        let id = req.body.id_class;
        const classeFounded = await Classe.findById(id)
        const comment = new Comment({

            id_class: classeFounded._id,
            comment: req.body.comment,
            date_created: classeFounded.createdAt
        })
        try {
            const savedComment = await comment.save()
            return res.status(201).json(savedComment)
        } catch (err) {
            return res.status(500).json({ message: err })
        }
    }

    static async getAllComments(req,res){
        try {
            const allComments = await Comment.find()
            return res.status(200).json(allComments)
        } catch (err) {
            console.log(err)
            return res.status(500).json({ message: err })
            
        }
    }

    static async getCommentsByClass(req, res) {
        let id = req.params.id_class
        try {
            const classComments = await Comment.findById(id)
            return res.status(200).json(classComments)
        } catch (err) {
            return res.status(500).json({ message: err })
        } 
    }

}
module.exports = CommentController


Read more here: https://stackoverflow.com/questions/64897458/routes-not-return-the-json-and-return-error-nodejs

Content Attribution

This content was originally published by Bussola at Recent Questions - Stack Overflow, and is syndicated here via their RSS feed. You can read the original post over there.

%d bloggers like this: