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

217
Vistas
Array Find() method is always returning 'undefined' while searching data with respect to URL parameters

I want to show the data from the array of whole data which should satisfy the condition that the requested id matches with the id of the data. But every time I try the below code, I only get 'undefined' as an output from the find method.

Code


    import express from 'express';
    import data from "./data.js"
    
    const app=express(); 
    app.get('/products/api/:id',(req,res)=>{
        const product = data.products.find((x) => x.id === req.params.id);
          if (product) {
               res.send(product)
          }
          else{
              res.status(404).send({message:`Product Not Found${req.params.id}`});
          }
      }) app.get('/products/api',(req,res)=>{
        res.send(data.products) 
      }) app.get('/',function(req,res){
        res.send("server is ready") 
      }) const port=process.env.PORT || 5000 app.listen(5000,()=>{
        console.log(`serve at http://localhost:${port}`);
     })

about 4 years ago · Juan Pablo Isaza
2 Respuestas
Responde la pregunta

0

Your code is missing body parsing middleware. Try adding an

app.use(express.json({ extended: true })) before your routes.

Also please check if the id from data.products.find((x) => x.id === req.params.id); is of type string. It might be a number or an ObjectId mongoose object id.

about 4 years ago · Juan Pablo Isaza Denunciar

0

All you need to do is,

change this:

const product = data.products.find((x) => x.id === req.params.id);

to:

const product = data.products.find((x) => x.id == req.params.id);
about 4 years ago · Juan Pablo Isaza 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