我有一个员工阵列,
[
{
"employeeNum": 1,
"firstName": "Foster",
"lastName": "Thorburn",
"email": "fthorburn0@myCompany.com",
"SSN": "935-74-9919",
"addressStreet": "8 Arapahoe Park",
"addresCity": "New York",
"addressState": "NY",
"addressPostal": "20719",
"maritalStatus": "single",
"isManager": true,
"employeeManagerNum": null,
"status": "Full Time",
"department": 2,
"hireDate": "4/30/2014"
},
{
"employeeNum": 2,
"firstName": "Emmy",
"lastName": "Trehearne",
"email": "etrehearne1@myCompany.com",
"SSN": "906-43-6273",
"addressStreet": "66965 Shelley Circle",
"addresCity": "New York",
"addressState": "NY",
"addressPostal": "33605",
"maritalStatus": "single",
"isManager": true,
"employeeManagerNum": null,
"status": "Full Time",
"department": 2,
"hireDate": "6/25/2016"
},
etc
]
我server.js
档案中的路线,
app.post("/employee/update", (req, res) => {
console.log(req.body);
data.updateEmployee(req.body).then((data) => {
res.redirect("/employees");
}).catch(function(err) {
console.log("An error was encountered: " + err);
})
});
updateEmployee()
定义为,
module.exports.updateEmployee = function(employeeData) {
return new Promise((resolve, reject) => {
let flag = false;
for (let i = 0; i < employees.length; i++) {
if (employees[i].employeeNum == employeeData.employeeNum) {
employees[i] = employeeData;
employees[i].employeeNum = i + 1;
flag = true;
}
}
if(flag) {
console.log("NO DATA FOUND");
reject("No data found");
} else {
resolve();
}
});
}
console.log(req.body);
行在我的app.post
路线中正确执行。这与employeeNum
有关吗?我尝试过做这样的事情,
for (let i = 0; i < employees.length; i++) {
if (employees[i].employeeNum == employeeData.employeeNum) {
employees[i] = employeeData;
employees[i].employeeNum = i + 1;
flag = true;
console.log(employees[i] + "\nDKJSFSFS");
}
}
但循环中的console.log(...)
甚至不打印..
有什么想法吗?
答案 0 :(得分:0)
想出来
在我更新员工的表单中,employeeNum
不是可访问的值;它作为一个索引。因此,我无法比较
(employees[i].employeeNum == employeeData.employeeNum)
我通过比较SSN值来修复它。
for (let i = 0; i < employees.length; i++) {
if (employees[i].SSN == employeeData.SSN) {
employees[i] = employeeData;
employees[i].employeeNum = i + 1;
flag = true;
}
}