2
\$\begingroup\$

This is the stripped down version of my update action for a Posts (blog post) controller:

Posts.update = function(req, res){
 _.extend(req.post, {
 title: req.body.title
 , author: req.body.author
 , body: req.body.body
 });
 post.save();
};

This way feels a bit clumsy and repetitive, because I have similar code in another method, but I don't want to just straight up _.extend(req.post, req.body) because it may allow someone to change fields that shouldn't be changed in my models.

Is there a better middle ground?

Jamal
35.2k13 gold badges134 silver badges238 bronze badges
asked Jul 24, 2012 at 0:10
\$\endgroup\$

1 Answer 1

3
\$\begingroup\$

How about _.pick()?

Posts.update = function(req, res){
 _.extend(req.post, _.pick(req.body, 'title', 'author', 'body'));
 post.save();
};
answered Jul 25, 2012 at 1:50
\$\endgroup\$

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.