Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

SOLVED: How to GET a single record from collection of data on routed request with params?

styler:

I'm trying to create a simple node server that on a GET request for the following paths, returns some static JSON:

/employees /employees/:id

I've created 2 GET requests, but my problem comes when trying to GET 1 Employee as the employees data doesn't exist, I've tried using a variable to store the data in /employees but this doesn't seem to work. Do I need to combine and check if an id exists?


let employees = []

app.get('/employees', (req, res) => { let data = generate(100) employees = data


console.log(req.body)

res.json({
"status": "success",
"data": data,
"message": []
}).end()
})

app.get('/employees/:id', (req, res) => {
let employee = _.find(employees, {"id": req.params.id}) //employees doesn't exist

res.json({
"status": "success",
"data": employee,
"message": []
}).end()
})



Posted in S.E.F
via StackOverflow & StackExchange Atomic Web Robots
This Question have been answered
HERE


This post first appeared on Stack Solved, please read the originial post: here

Share the post

SOLVED: How to GET a single record from collection of data on routed request with params?

×

Subscribe to Stack Solved

Get updates delivered right to your inbox!

Thank you for your subscription

×