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

211
Views
API call giving empty array in nodejs

This is 'product.js' file, and when ever I am calling the api '/achaar/products/1', the value of val is giving empty in console. Other api calls like '/achaar/products' is working fine, but on calling with id this is not working.

const express = require('express')
const router = express.Router();
const json_data = require('./test_data');
const cons = require('./constants')

/*
url is : /achaar/products
replace test data with database datas
*/

router.get(cons.URLS.all_product,(req,res) => {
res.json(json_data)
})

router.get(cons.URLS.all_product+'/:id',(req,res) => {
console.log(req.params.id)
var val = json_data.achaar.filter(function (x) { return x.id === req.params.id })
console.log(val)
})

module.exports = router

Here is my test_data.js file

 {
 "achaar" : [
    {
        "id" : 0,
        "name" : "Ginger Tangi",
        "cost" : 2000
    },
    {
        "id" : 1,
        "name" : "Mango Khatta",
        "cost" : 1200
    },
    {
        "id" : 2,
        "name" : "Gaas Dhari",
        "cost" : 3000
    },
    {
        "id" : 3,
        "name" : "Cream Tangy",
        "cost" : 1000
    }
 ]
}
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Because you use === and it was check value and type is equal

You should convert req.params.id to number because id of data is number.

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!