javascript - Trying to delete entry from MongoDB database using Express.js & Jade -
i created app adds , removes 'case studies'. able add entries easily, when try delete entry, redirected of form page form action url appended end. entry in fact deleted when go original form page, ideally i'd able stay on same page add/delete entries.
form.jade
form(method="post", action="/form/#{entry._id}?_method=delete") input(type="submit", value="delete")
form.js
/* delete case study */ router.delete('/:id', function (req, res) { entry.findbyid(req.params.id) .exec(function(err, entries) { // changed `if (err || !doc)` `if (err || !entries)` if (err || !entries) { res.statuscode = 404; res.send({}); } else { entries.remove(function(err) { if (err) { res.statuscode = 403; res.send(err); } else { res.send('deleted entry link: ', entries.link); } }); } }); });
here link app on heroku, github repo:
https://hidden-wave-1121.herokuapp.com/form
https://github.com/hlmurray/express-app
any here great, thank you!
you can use javascript http request instead of form post/submission , wire onclick function on delete button, heres stackoverflow question covers http requests.
input(type="button", onclick="deleteitem({entry._id})") function deleteitem(theid) { var xmlhttp = new xmlhttprequest(); xmlhttp.open( "delete", '/form/'+ theid, false ); xmlhttp.send( null ); return xmlhttp.responsetext; }
also looks you'll need update route in form.js reads /forms/:id.
that won't rid of html on page though, you'll need more javascript delete dom element. when work mean stack, form posts , calls using angular $http method, way don't page changes. still use forms, instead of standard submit, use ng-submit, prevents page changes.
hope helps, let me know if can include else, , i'll update answer.
Comments
Post a Comment