The code in Listing 26.8 implements the deleteUser
route. This route finds the user in the MongoDB database and then calls the remove()
method on the User
object to remove the user from the database. Also notice that req.session.destroy()
is called to remove the session because the user no longer exists.
73 exports.deleteUser = function(req, res){
74 User.findOne({ _id: req.session.user })
75 .exec(function(err, user) {
76 if(user){
77 user.remove(function(err){
78 if (err){
79 req.session.msg = err;
80 }
81 req.session.destroy(function(){
82 res.redirect('/login'),
83 });
84 });
85 } else{
86 req.session.msg = "User Not Found!";
87 req.session.destroy(function(){
88 res.redirect('/login'),
89 });
90 }
91 });