0
app.post("/delete", function(req,res){
    var deluser = req.body.usernamedel;
    var reason = req.body.reason;
    User.remove({name: deluser}, function(err, user){
        if(err){
            console.log(err)
        } else {
            console.log(user);
        }
    }).exec();
});

I'm trying to get this post request to pull the name from the page and then remove it from the database, but it doesn't seem to remove it. All it shows in the console is this -{ n: 0, ok: 1 }- which I don't know what means. How do I make the command actually delete the user?

3
  • What it means is the n: 0 essentially says that nothing actually matched the query condition given in argument, so there is no data in your collection matching { name: deluser } for the value you are providing. In all likelihood, you actually issued the same request multiple times, and initially it would have been { n: 1, ok: 1 } indicating that 1 ( or possibly a larger number of matches ) document was matched and removed. The ok: 1 of course means there was no error. Commented Apr 16, 2018 at 0:55
  • If you think the data is still in the collection, then whatever you are supplying to req.body.usernamedel is not actuallly what you think it is. Check your collection, and then also check the input you are providing with something like console.log(deluser) and see what the actual input value being sent really is. Commented Apr 16, 2018 at 0:56
  • As a final beginner note, you are using mongoose and that means your User model is expecting "by default" to see a collection named users. If your actual collection is named user or even User in MongoDB, then there is another way to tell mongoose to use that name instead. Commented Apr 16, 2018 at 0:58

1 Answer 1

1

Note: Use appropriate http verbs according to the task. Please do not use POST for DELETE operation. This will lead to inconsistent REST implementation.

I hope below code will solve will your query.

app.delete("/delete", (req, res, next){
    const userToDelete = req.params.username;
    User.findOneAndRemove({name: userToDelete})
    .then(deletedUser => res.json(deletedUser))
    .catch(err => next(err));
});
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.