4

Thank you in advance, Im really stuck on this!

I have a User object in the database like such:

{
    "_id": {
        "$oid": "5a83470e722c1e00142e2216"
    },
    "first_name": "Test",
    "last_name": "User",
    "password": "$2a$10$FdsPkcjeLOpwfCRCHOSg6eqVDrDlbpi330tyG/Z.XPl3dKzX5K3s.",
    "email": "test.com",
    "role": 3
}

So I'm calling a function that is supposed to update an object, like a user just edited their profile. This is the body of my request:


{
    "userID": "5a83470e722c1e00142e2216",
    "employee": {
        "first_name": "TestNEW",
        "last_name": "UserNEW",
        "password": "$2a$10$FdsPkcjeLOpwfCRCHOSg6eqVDrDlbpi330tyG/Z.XPl3dKzX5K3s.",
        "email": "NEWUSER.com",
        "role": 3
    }
}

When I update the object, I want to $set or $update the entire user object, except for the _id. So, Im using this:

User.findOneAndUpdate({ _id: new ObjectID(req.body.userID)},
 { $set: { ...req.body.employee } }, function(err, user)...etc etc

But that doesnt work of course! So Im wondering how I can $set or $update (not sure which) all of the properties on the Object except for its _id, without using brute force like

{ $set: { first_name: req.body.employee.firstName, last_name: req.body.employee.lastName, ...etc etc etc } }
I know this way works, but I want to do it in a cooler way of course, can anybody help? Thank you!
  • There is no spread [*operator*](http://ecma-international.org/ecma-262/8.0/#sec-unary-operators), there is [*spread syntax*](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Spread_syntax). – RobG Feb 14 '18 at 23:21
  • @RobG Spread operator is accepted as well. Companys use Spread Operator phrase on their tutorials. – Ele Feb 14 '18 at 23:36
  • 2
    Try `User.findOneAndUpdate({ _id: new ObjectID(req.body.userID)}, req.body.employee, { overwrite: true } );` – s7vr Feb 15 '18 at 01:17
  • @Ele—your unnamed sources are wrong, see [*Is …foo an operator or syntax?*](https://stackoverflow.com/questions/44934828/is-foo-an-operator-or-syntax) – RobG Feb 15 '18 at 02:57
  • @RobG for example [Redux js](https://github.com/reactjs/redux) - [Tutorial with Spread operator](https://redux.js.org/docs/recipes/UsingObjectSpreadOperator.html) - [Eventbrite - Rest & Spread Operators](https://www.eventbrite.com/engineering/learning-es6-rest-spread-operators/) – Ele Feb 15 '18 at 14:06
  • 1
    @Ele—please see in the comments section for the [*post I linked to*](https://stackoverflow.com/questions/44934828/is-foo-an-operator-or-syntax). If contributors like [Bergi](https://stackoverflow.com/users/1048572/bergi) (+300k rep) and [Felix Kling](https://stackoverflow.com/users/218196/felix-kling) (+480k rep) agree that it's not an operator, you'll need to come up with a reference of greater authority. ;-) – RobG Feb 16 '18 at 04:04
  • @RobG reduxjs? Eventbrite? Real solutions used by thousands in the industry vs two contributors in SO? Anyway, have a good day :-) – Ele Feb 16 '18 at 06:05
  • @Ele—sorry, but contributions from individuals (who do not represent the platforms referenced) don't trump the language specification whose authors ([*TC39*](https://github.com/tc39/ecma262#ecmascript)) are people like Brendan Eich who invented the language, or MDN, which has thousands of contributors. Your choice though I guess. You might discuss it on the [ES-Discuss mailing list](https://esdiscuss.org/). – RobG Feb 16 '18 at 09:00

0 Answers0