I am working on an example, I am trying to get a single employee instance from this express example, but when I type, localhost:3000/1, I am getting an empty array. does anyone know what I am doing wrong here?
'use strict';
const express = require('express');
const app = express();
var employees = [
{
"EmployeeID" :1 ,
"EmployeeName" : "RNA Team",
"Salary" : "200000",
"Address" : "Bangalore"
},
{
"EmployeeID" :2 ,
"EmployeeName" : "Mahesh Samabesh",
"Salary" : "100000",
"Address" : "Hydrabad"
},
{
"EmployeeID" :3 ,
"EmployeeName" : "Rui Figo",
"Salary" : "50000",
"Address" : "Dallas"
},
{
"EmployeeID" :4 ,
"EmployeeName" : "Indradev Jana",
"Salary" : "456789",
"Address" : "Los Angles"
},
{
"EmployeeID" :5 ,
"EmployeeName" : "Suresh Shailesh",
"Salary" : "1234567",
"Address" : "Patna"
}
];
//Get the employees records
app.get('/', function(req, res){
res.send(employees);
});
//run the server
var server = app.listen(3000, function(){
var host = server.address().address;
var port = server.address().port;
console.log('Server started and listening at:> http://%s:%s', host, port);
});
//Get single employee record
app.get('/:EmployeeID', function(req, res){
var employeeID = req.params.EmployeeID;
//Get Employee Records whose EmployeeID = get the EmployeeID at runtime
var filteredEmployee = [];
for(var i=0; i < employees.length; i++){
if(employees[i].EmployeeID == employeeID){
filteredEmployee.push(employees[i]);
}
} //end Loop
employees = filteredEmployee;
console.log(filteredEmployee);
res.send(employees);
});
req.params.EmployeeIDwhen you try it? One possibility here is that you aren't passing in the right ID so thus you never find a match. Also, you should not be changing theemployeesarray just because you're searching it. That will damage the data for the next time you want to search it.