Deleting an entry through a button click using mongoldb, mongoose, jade and express Deleting an entry through a button click using mongoldb, mongoose, jade and express mongoose mongoose

Deleting an entry through a button click using mongoldb, mongoose, jade and express


One way to do this is to set in the each button an id of the document that you want to remove.

//jadetd   button.remove-doc.btn.btn-danger.col-xs-12(data-id="#{user.id)") X

and add an event listener to send an ajax request to delete the user:

<script>   $('buttons.remove.doc').on('click', function() {      var userId = $(this).attr('data-id');      $.ajax({         method: "POST",         url: "/users/delete",         data: {"userId": userId},         success: function(result) {            if(/* check if it is ok */) {                location.reload();            }         }      })   });</script>

in node you will have something like this:

app.post('/users/delete', function(req, res, next) {   var userId = req.body.userId || req.query.userId;   userSchema.remove({_id: userId}, function(err, res) {       if (err) { res.json({"err": err}); } else { res.json({success: true});   });});