2

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)
Jibran Yousuf
  • 77
  • 1
  • 10

2 Answers2

0

In your deleteUser function your not accessing the function parameter, but you try to access a local variable with the this operator. This variable is not existing, which is why you get an undefined in the link.

Remove the this part, so that the function parameter is used with the url instead.

deleteUser(_id){
    let headers = new Headers();
    return this.http.delete(this.deleteurl+_id)
    .map(res => res.json());
}
Benedikt Schmidt
  • 2,178
  • 16
  • 20
  • i just made these changes the console result is still same P.S. the error got somewhat like this ** DELETE http://localhost:3000/users/delete/localhost:3000/users/delete/undefined 404 (Not Found) ** – Jibran Yousuf Dec 25 '17 at 02:15
  • I just notice that you have two `deleteUser` functions, one in the service and one in the component. The problem with the `this` operator is in the service. Also in this service function you are expecting the ID to delete, nothing more, but you're currently giving the full link *plus* the id. Try to call the service function with `this.authService.deleteUser(_id).subscribe` – Benedikt Schmidt Dec 25 '17 at 02:20
  • Changes Updated: I changed the button function to this so it will help to distinguish `OnDeleteUser(_id){ this.authService.deleteUser(_id).subscribe(data=> { console.log(data,"data from db") this.user(); }, err => { console.error(err, "error" ) }` and service deleteUser function to `deleteUser(_id){ let headers = new Headers(); return this.http.delete(this.deleteurl+_id) .map(res => res.json()); }` and this is what i am passing in the component.ts file _id:'5a3aaa1da685361dccddbbad'; – Jibran Yousuf Dec 25 '17 at 02:28
  • alright, still errors from the function call? I'm pretty sure the output should be different after these changes – Benedikt Schmidt Dec 25 '17 at 02:37
  • ** DELETE http://localhost:3000/users/delete/undefined 500 (Internal Server Error) ** The error is different now but it is there :-/ – Jibran Yousuf Dec 25 '17 at 02:39
  • You're calling your `OnDeleteUser` function from your button. You're giving an _id parameter to this function call: `(click)="deleteUser(_id)"`. Where do you get this _id from? is your button inside a table or a list? You can test if this _id is not correctly used by putting a `console.log(_id)` right at the beginning of your `OnDeleteUser` function. If it's already undefined there, than you are not using a correct variable inside your button `click` function. Can you update your code by adding additional html code from your button to see where the _id comes from? – Benedikt Schmidt Dec 25 '17 at 02:46
0

500 Internal Server error coming from these line.

User.findByIdAndRemove(req.params.id, function (err, user) {
    if (err) { 

    return res.status(500).send("There was a penter code hereroblem deleting the user.");


}
Rahul Singh
  • 150
  • 1
  • 7