Skip to content

Commit 8add148

Browse files
committed
Fix delete by id
1 parent f789094 commit 8add148

File tree

2 files changed

+5
-4
lines changed

2 files changed

+5
-4
lines changed

src/app.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ app.use((err, req, res, next) => {
2424
if (!err) {
2525
return next();
2626
}
27+
console.err('ERROR', err);
2728
res.status(err.status || 500);
2829
res.send({ message: err.message });
2930
return res;

src/repository/employeesRepository.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
let employeesData = [{
1+
const employeesData = [{
22
id: 1,
33
name: 'Mahesh',
44
department: 'IT',
@@ -32,11 +32,11 @@ function getById(employeeId) {
3232

3333
// delete employee by id
3434
function deleteById(employeeId) {
35-
const employee = employeesData.filter(item => item.id === employeeId);
36-
if (employee.length === 0) {
35+
const employeeIndex = employeesData.findIndex(item => item.id === employeeId);
36+
if (employeeIndex === -1) {
3737
throw new Error(`Employee Resource with id: ${employeeId} not found`);
3838
}
39-
employeesData = employeesData.filter(item => item.id !== employeeId);
39+
employeesData.splice(employeeIndex, employeeIndex + 1);
4040
return { id: employeeId };
4141
}
4242

0 commit comments

Comments
 (0)