I want to simply delete a user from the database. using MEAN Stack, mlab.
This is my delete API
router.delete('/delete/:id', function (req, res) {
User.findByIdAndRemove(req.params.id, function (err, user) {
if (err) {
return res.status(500).send("There was a penter code hereroblem deleting the user.");
} else {
res.status(200).send("User "+ user.username +" was deleted.");
res.json({ user: user });
}
});
});
this is the service i am calling
deleteUser(_id){
let headers = new Headers();
return this.http.delete(this.deleteurl+this._id)
.map(res => res.json());
}
my HTML Delete Button
button type="button" class="btn btn-primary" (click)="deleteUser(_id)">Delete User</button>
and this is the deleteuser
function I am calling on the button in HTML
deleteUser(_id){
this.authService.deleteUser('localhost:3000/users/delete'+'/'+_id).subscribe(data=> {
console.log(data,"data from db")
this.user();
},
err => {
console.error(err, "error" )
}
)};
Currently, I am putting the id as the id number of my document, i.e. hardcode the value of id
and this is the error I am getting
DELETE http://localhost:3000/users/delete/undefined 500 (Internal Server Error)