Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

214
Views
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 answers
Answer question

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 Report

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!