Mar 9, 2014

How to make a project with Node.js & MongoDB (CRUD) - Part 2 (Update and Delete)



In this post we will continue the previous project in which we were creating a CRUD application with Node.js and MongoDB, helped with the Express framework and Mongoose libraries to read and write data. Now we will see how this information can be updated or deleted




If you have not read the previous post:

- How to make a project with Node.js & MongoDB (CRUD) - Part 1



Start the DataBase

1 - Open other window of your terminal and headed to the Data folder in your project.

2 -  write: 
   $ mongod --dbpath  folder_data_url*     *change folder_data_url for your folder data url
 Warning: You can't close this terminal window.


UPDATE


View Contacts

First we have to change the main view by adding the option to modify each of our entries, add "a(href='/contacts/#{info.id}/update') Update" in the end like:


...
- each info in data
tr
td #{info.name}
td #{info.email}
a(href='/contacts/#{info.id}/update') Update
becareful with the tabulated, jade is very sensitive with them



Routes

We need to create two new routes: the first collects the data from the main view to bring the form with the necessary information and the second includes the information we have modified and updated in the database.

Include them in the server app.js just below which we had already created:

// ... code

app.post('/contacts',function(req, res){
newinfo = new info({ name: req.body.name, email: req.body.email});
newinfo.save(function(err){
if (err) {throw error;}
else {res.redirect('/contacts');}
});

});

app.get('/contacts/:id/update',function(req, res){
       info.findById(req.params.id,function(err,data){
              res.render('update', {
                      title: 'Update Contact', data:data });
        });
});

app.post('/contacts/update',function(req, res){
       info.findById(req.body.id, function(err,data){
                data.name = req.body.name;
                data.email = req.body.email;
                data.save(function(err){
                      if (!err) {res.redirect('/contacts');}
                      else { throw err;}
               });
       });
}); 


// ... code
View update.jade

Create a new view with a form, this form has printed information that we want to change to later send to the second route we have created on the server:






DELETE


View Contacts

First we have to change the main view by adding the option to modify each of our entries, add "a(href='/contacts/#{info.id}/update') Update" in the end like:


...
- each info in data
tr
td #{info.name}
td #{info.email}
a(href='/contacts/#{info.id}/update') Update
                                a(href='/contacts/#{info.id}/delete') Delete

becareful with the tabulated, jade is very sensitive with them


Routes

We need create a new route. 


// code...

app.post('/contacts/update',function(req, res){
       info.findById(req.body.id, function(err,data){
                data.name = req.body.name;
                data.email = req.body.email;
                data.save(function(err){
                      if (!err) {res.redirect('/contacts');}
                      else { throw err;}
               });
       });
}); 

app.get('/contacts/:id/delete',function(req, res){
      info.findById(req.params.id,function(err,data){ 

             data.remove(function(){ 
                     res.redirect('/contacts'); 
             }); 
      }); 
});

// code...


and that's it:


Run de App

Write in the terminal:   $ node app  and open your browser in localhost:3000/contacts




I hope you liked it, and if you have any questions feel free to write me:







No comments:

Post a Comment