Sunday 15 June 2014

How to create an MVC form using MongoDB, Node.js(+express) and Jade -


I have been working with creating a simple CRUD app using the up stack.

However, with me lack of knowledge can be prevented from doing things in an easy fashion.

My main concern with the update is that I have an object that is loaded from the database. The fields need to be mapped in one form, and when the contents of the form are modified and saved, then the database should be updated.

I found it very easy to render, and dynamically populated using ZE, however, on the way back, I used jQuery to rebuild my "manually" object. I am sending a Strataced JSON presentation of the form; Before storing it back into the database.

Is there an automatic device that mapped it for me? Looking at a JSON object and a form, it will handle the population and save the information back in the model.

For reference, I'm coming from a SpringMVC + hibernate background.

The tutorial for the tutorial or even a simple example would be OK. thank you in advanced!

Like others, I would definitely recommend it to help me when I try to learn the same stack was doing. On the server you can have something like this:

  // defined schema var userSchema = mongoose.Schema ({name: 'string', Description: 'string', email: 'string', Date: {type: date, default: date, now},}); // Instant DB model var user = mongos Model ('user', user schema); // Post client export from Adds json from client post in addUser = function (req, res) {// req.body / unless that json field name is similar to schema field name, this data will automatically map new user = new user (req. Body); New user.save (error) {if (err) {return res.json ({error: "Error saving new user"});} else {console.log ("Success in adding a new user"); Res.json new user); }}); }; // PUT from client export Updduser = function (req, body) {// updated user json var updatedUser = req.body; // Here we can see that user by email field User.findOne ({'email': updatedUser.email}, function (mistake, user) {if (err) {return res.json ("error:" user Error in fetching "});} Else {//. We successfully found a user in the database, Update individual fields: user.name = updatedUser.name; user.save (function (err) {if (err) {return res.json ({Error: "Error updating user"});} other {console.log ("success in updating user"); res.json (updatedUser);}})}}}); }; Edit * Apparently, Mongoose actually has a search and update method that essentially does the same thing as I wrote above to update a model. You can read about it   

No comments:

Post a Comment