I want to map json data from request.params to model in my controller class. It have some action in controller before save model to database.
Does it has the solution to auto map json data to model object without manually do this?
var email = req.body.email; var phone = req.body.phone;
If I understand your question correctly, then yes.
I'll often build forms like this:
<input type="text" name="data[email]" value="ex@example.com">
<input type="text" name="data[phone]" value="1234567890">
In your controller:
var data = req.param('data')
// data is now = {email : 'ex@example.com', phone : '1234567890'}
And when updating database:
User.create(data).exec(funtion(e,r){
console.log(e||r)
// if there is no error, object r should contain:
// {
id : <id>,
email : 'ex@example.com',
phone : '1234567890',
createdAt : <date>,
updatedAt : <date>
}
})