This is the experience of continuing to learn on the basis of previous exercises. After the page brushes out the data to want to perform the deletion to some content, here uses the jquery Ajax, passes through the Ajax to a unique value, like collection inside _id, then receives this parameter in the node handler, calls the Remove method to perform the deletion operation. The specific code is as follows:
1, my Ajax code is written in a page.
$ ('. My-delete '). Click (function () {
var id=$ (this). attr ("Data-id"); In the page I direct the _id value of the data to the Data-id
$.ajax ({
type: ' Post ',
URL: "/msgdelete",
data:{id:id},
Success:function (data) { //In my writing, here success is not executed, ran directly to the message handler, successfully deleted over the
if (data) {
Console.log ( ' Successful AH ');
else{
Console.log ("return value is null");}})
This process URL is "/msgdelete", so you need to modify App.js add Msgdelete processing event, I am on the current page to perform the delete operation, so the Turn page is also the current page.
App.get ('/msgdelete ', Message.del);
App.post ('/msgdelete ', message.delmsg);
Modify the Del and Delmsg methods inside the mesage.js as shown below.
Exports.del = function (req,res) {
res.render (' home ', {title: ' Delete Message '})
}
exports.delmsg = function (req,res) {
msgmodel.remove ({_id:req.body.id},function (err,data) {
if (!err) {
res.send (' True //delete succeeds to return true for the data received by the success function of the front-end Ajax, where the format of the send can be defined on its own
}else{
res.send (' false ');
}
})
}
The delmsg here is really the process of implementing the delete operation, the Remove () method of two parameters, the first is the condition, the second is the callback function, here I want to delete the specified ID of the data, through the
req.body.idTo get the previous page Ajax incoming parameters, after the deletion is complete, recall the Find method to look up the data of the database to see if the result is correct, docs for the returned array column.
Specific what delete, phase to remove the hidden ah and so on do not write, the need for their own research, anyway, the return value has been given out.