Implementing the Delete User Route

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.

Listing 26.8 users_controller.js-deleteUser: Implementing the route to delete users for the Express server


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   });


..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.119.159.178