0
const user = new db.User({
        firstName: req.body.firstName,
        lastName: req.body.lastName,
        password: req.body.password,
        email: req.body.email,
        dateCreated: req.body.dateCreated
    })

I know there's a way to assign values to an object by giving the attributes the same name as their source but I'm not sure how this would work.

spanky
  • 2,698
  • 6
  • 9
TheGreyBearded
  • 337
  • 4
  • 14

3 Answers3

2

You could deconstruct all of these values above to simplify it:

const { firstName, lastName, password, email, dateCreated } = req.body

Then all you would need to do is the following:

const user = new db.User({
    firstName,
    lastName,
    password,
    email,
    dateCreated,
})
Luke Glazebrook
  • 580
  • 2
  • 14
0

It seems like a good use case for Automapper. It will map like names and can collapse variables.
http://automapper.org/

biznetix
  • 94
  • 5
-1

You could also do it as a functional one-liner, but even in this case you still have to repeat property names.

const user = new db.User(
  (({firstName, lastName, password, email, dateCreated}) =>
   ({firstName, lastName, password, email, dateCreated}))(req.body)
);
Sergei
  • 6,372
  • 1
  • 21
  • 32