Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

120
Vistas
Check if req.param.dogid / req.param.dogname in Express Mongoose

I have 2 GET functions in the same file.

one for dogid and the other for dogname.

//Operations for api/dogs/{dogId} route
//get operation
 router.get('/:dogId', async (req,res)=>{   
      try{
          const dog = await Dogs.findById(req.params.dogId);
          res.json(dog);
      }catch(error){
          res.json({message:error});
      }
  
  })

And

//Operations for api/dogs/{dogName} route
 //get operation
 router.get('/:dogName', async (req,res)=>{   
      try{
          const dog = await Dogs.find({'name': { $in: req.params.dogName}})
          res.json(dog);
      }catch(error){
          res.json({message:error});
      }
  
  })

When I try npm start it doesn't give any problem.

But in Postman when I send GET operation for http://localhost:3000/api/dogs/Sasha

then it shows the following:

{
    "message": {
        "stringValue": "\"Sasha\"",
        "valueType": "string",
        "kind": "ObjectId",
        "value": "Sasha",
        "path": "_id",
        "reason": {},
        "name": "CastError",
        "message": "Cast to ObjectId failed for value \"Sasha\" (type string) at path \"_id\" for model \"Doggies\""
    }
}

The code for getting the data with dogname is already present. But since the get function for the dogid route is written before the dogname route it's showing this error.

over 4 years ago · Santiago Trujillo
2 Respuestas
Responde la pregunta

0

The problem is that GET api/dogs/:dogId is handling the request since it is written before GET api/dogs/:dogname.

To solve this problem try changing your api design or using different HTTP request methods. For example :

POST api/dogs/:dogId
GET api/dogs/:dogName

or GET api/animals/dogs/:dogId
   GET api/dogs/:dogName

But don't do this:

   GET api/dogs/:dogId
   GET api/dogs/:dogName
over 4 years ago · Santiago Trujillo Denunciar

0

As @Mohammad Ismail already pointed out, you need to use different HTTP Method to differentiate between 2 routes.

There's another approach is to use query instead of param
You send the request like localhost:3000/api/dog?dogName=abc
Then in your code: req.query.dogName will get you the value abc

over 4 years ago · Santiago Trujillo Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda