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,648
  • 5
  • 9
TheGreyBearded
  • 317
  • 4
  • 13

3 Answers3

1

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
  • 540
  • 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
  • 5,942
  • 1
  • 19
  • 32