我正在研究一个例子,我试图从这个明确的例子中获得一个雇员实例,但是当我输入localhost:3000/1时,我得到一个空数组。有谁知道我在这里做错了什么?
'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);
});
&#13;
答案 0 :(得分:2)
你真的不应该在查询操作中改变你的数据。你可以这样做:
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` })
});
答案 1 :(得分:1)
您实际上正在改变您的数据。您的工作方式只能工作一次,然后您的员工数组会发生变化。因此,只需删除employees = filteredEmployee;
,然后直接发送res.send(filteredEmployee);
。
答案 2 :(得分:1)
您正在重新分配您的员工对象。
employees = filteredEmployee;
将您的员工对象设置为单个找到的员工。所以它会第一次工作,而且确实如此。然后任何后续尝试都只会有一名员工。
您只想在循环后执行res.send(filteredEmployee)
。
您的代码也会将您的浏览器请求作为员工参数提交给/favicon.ico
。
添加它会阻止:
app.get('/favicon.ico', function(req, res) {
res.send(204);
});
修复示例:
'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);
});