node.js - Recommended way to delete object in MongoDB based on a route -
new mean stack development, loving it! want ask question in such way isn't opinion based, forgive me if is.
lets have admin dashboard lists out objects in mongodb collection. each item has delete button has href of scheme: href="/admin/ministry/delete/:id"
:id
id of object in db delete.
right have route setup routes request controller, i'm wondering if best way delete object. example, once click on delete button object, gets deleted , dashboard, url remains this: http://localhost:3000/admin/ministry/delete/554b88546d280ab11603b062
so question is, suggestions have route , controller when comes deleting object db.
router
var admin_ministry_controller = require('./controllers/admin/ministry_controller.js'); app.get('/admin/ministry/delete/:id', ministry_controller.delete);
controller
var mongoose = require('mongoose'); var ministry = mongoose.model("ministry"); exports.delete = function(req, res) { ministry.findone({_id:req.params.id}).exec(function(err, ministry){ if(ministry) { ministry.remove(); } var query = ministry.find(); query.exec(function(err, doc) { res.render('admin/ministry', {title: 'next steps | ministry', msg: "deleted ministry", ministries: doc}); }); }); }
i suggest use restful api structure. take @ api like:
in case, delete ministry
, users should send delete
request /api/admin/ministry/:id
.
and router should this:
app.delete('/admin/ministry/:id', ministry_controller.delete);
Comments
Post a Comment