0

when I click delete button I find particular data with id in chrome dev. tool but never delete data from database. I get an error like

DELETE http://localhost:3000/employee/5c29b718b9bc521ef8a91c3b 500 (Internal Server Error) core.js:14597 ERROR HttpErrorResponse {headers: HttpHeaders, status: 500, statusText: "Internal Server Error", url: "http://localhost:3000/employee/5c29b718b9bc521ef8a91c3b", ok: false, …}

controller file (node):

router.delete('/:id', (req,res) => {
    if(!objectId.isValid(res.params.id)){
        return  res.status(400).send(`no record found for given id : ${ $res.params.id }`);
    } else {
        Employee.findByIdAndDelete(req.body.id, (error,docs) => {
            if(!error)
            {
                res.send(docs);
            } else {
                console.log('Error in  employee Delete :' + JSON.stringify(error,undefined,2));
            }
        });
    }
});

service file (angular)enter code here


component.ts file (angular)

deleteEmployee(_id:string)
{
    return this.http.delete(this.baseURL + `/${_id}`);
}


 // delete employee

onDelete(_id: string,form :NgForm){
    if(confirm('are you sure remove this data .. ?') == true){
        this.employeeService.deleteEmployee(_id).subscribe((res) => {
            this.refreshEmployeeList();
            this.resetForm(form);
            M.toast({html: 'delete  sucessfully', classes: 'rounded'});
        });
    }
}
YakovL
  • 7,557
  • 12
  • 62
  • 102
  • looks like you forgot to insert *service file (angular)*. Please remember to add code formatting to your code (4-space indentation, I've added it for you in the code pieces you already posted). What's more important, your first phrase is totally unclear. What delete button? What do you mean by *particular data*? – YakovL Dec 31 '18 at 12:20
  • by the way, your routing uses `'/:id'` while you are showing the response from `/employee/5c29b718b9bc521ef8a91c3b` which is a different path. Perhaps you just used the wrong url for deleting. What's the value of `this.baseURL`? – YakovL Dec 31 '18 at 12:27
  • Probably router must be prefixed with `/employee` when registering with app.use – Rajika Imal Dec 31 '18 at 13:36

0 Answers0