Reinier68
Reinier68

Reputation: 3252

Very simple get request yields in error: 'Cast to ObjectId failed'

So I was testing my backend with postman and have no clue what the heck is going on at the moment. I got a file users.js where I define some of my routes. I import that file into my app.js with:

// Bring in the Users route
const users = require('./routes/api/users');
app.use('/api/users', users);

So all the api requests within users.js are actually going to /api/users/......

I defined a simple route here to test it out.

const express = require('express');
const router = express.Router();

router.get('/', (req, res) => {
    res.send('hello world')
})

module.exports = router;

So if I send a request with postman to: http://localhost:5000/api/users/ I get back a response: hello world

If I ONLY change the endpoint to something like /test, I get an error message. In other words, If I change the route to:

const express = require('express');
const router = express.Router();

router.get('/test', (req, res) => {
    res.send('hello world')
})

module.exports = router;

And again send a request with postman to http://localhost:5000/api/users/test, I get back the following error message:

{
    "stringValue": "\"test\"",
    "kind": "ObjectId",
    "value": "test",
    "path": "_id",
    "reason": {},
    "message": "Cast to ObjectId failed for value \"test\" at path \"_id\" for model \"Project\"",
    "name": "CastError"
}

Does anyone have a clue what is going on? I cannot see why this doesn't work...

EDIT:

Okay, so I figured it out, but I still don't know why it works in scenario 1 and doesn't work in scenario 2:

This works:

router.get('/test', (req, res) => {
    res.send('hello world')
})

//load project
router.get('/:id', (req, res) => {
    Project.findById(req.params.id)
        .then(projectFound => {
            res.status(200).send(projectFound)
        })
        .catch(err => {
            res.status(500).send(err)
        })
})

This doesn't work:

//load project
router.get('/:id', (req, res) => {
    Project.findById(req.params.id)
        .then(projectFound => {
            res.status(200).send(projectFound)
        })
        .catch(err => {
            res.status(500).send(err)
        })
})

router.get('/test', (req, res) => {
    res.send('hello world')
})

Upvotes: 0

Views: 102

Answers (2)

Menelaos
Menelaos

Reputation: 26004

Order to Precedence of Controllers

:id is a variable. When you declare the router.get('/:id', (req, res) first, your test method never gets executed.

This is because :id takes the value of test. And your code doesn't like the 'test' id, so you get an error.

When you declare the other method first, router.get('/test' is found first, and executed first for any request that is /test. All other requests are forwarded to the :id method.

Answer to Initial Question

var express = require('express')
var app = express()

app.get('/test', function (req, res) {
  res.send('HELLO WORLD')
})

Source

Upvotes: 1

David Taylor
David Taylor

Reputation: 1

I got a file users.js where I define some of my routes.

How are the routes defined?

Given the error message, presumably it is expecting /api/users/{id}, where {id} is a number, not a string like "test".

Upvotes: 0

Related Questions