0

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);
});

1
  • What is req.params.EmployeeID when 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 the employees array just because you're searching it. That will damage the data for the next time you want to search it. Commented Feb 19, 2017 at 18:29

3 Answers 3

2

You really shouldn't mutate your data in a query operation. You could do something like this:

app.get('/:EmployeeID', (req, res) => {
  const employeeID = req.params.EmployeeID;
  // filter the employees without mutating the array, and get the first result
  const [employee] = employees.filter(e => e.EmployeeID === employeeID)
  // if there was a result, send it, otherwise send an error
  res.json(employee ? employee : { error: `Employee with id ${employeeID} not found` })
});
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks Balazs, I tried to get this to work, but not sure, what I did wrong here. Am I able to set an array as a const in ES5? or that is just a placeholder?
1

You're re-assigning your employees object.

employees = filteredEmployee;

will set your employees object to be the single found employee. So it will work the first time, and it does. Then any subsequent tries it will only have a single employee in it.

You just want to do res.send(filteredEmployee) after your loop.

also your code is picking up your browsers request to /favicon.ico as an employee param.

adding this will block that:

 app.get('/favicon.ico', function(req, res) {
     res.send(204);
 });

Fixed example:

'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);
});

app.get('/favicon.ico', function(req, res) {
    res.send(204);
});

//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
    console.log(filteredEmployee);
    res.send(filteredEmployee);
});

Comments

1

You are actually mutating your data. The way you are doing will work only for once and then your employee array will change. So just remove employees = filteredEmployee; and directly send res.send(filteredEmployee); instead.

1 Comment

Why would this fix anything?

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.