question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Confusion due to (proposed) spread operator on model instance

See original GitHub issue

See https://github.com/sebmarkbage/ecmascript-rest-spread for a description of the spread operator. It behaves much like Object.assign() and it iterates over the object’s own properties.

When I use {...obj} on model instance, I don’t get a copy of the plain object as I expected but instead a copy of all the mongoose internals.

So basically, it would be nice if that either worked, or if, in development at least, iterating over own properties would throw.

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Reactions:4
  • Comments:8

github_iconTop GitHub Comments

2reactions
vkarpov15commented, Feb 21, 2020

You can’t. The spread operator always creates a new object, so you can’t use it to update an existing object. Use Object.assign() as an alternative. Or, better yet, just use card.set(cardData), see Document#set()

1reaction
vkarpov15commented, Jul 7, 2016

By design. Use doc.toObject() on the rhs of the spread operator. A mongoose doc doesn’t really lend itself well to copying, too much syntactic sugar for change detection

Read more comments on GitHub >

github_iconTop Results From Across the Web

Confusion regarding Spread Operator in JS [duplicate]
So basically I have list of objects. I am iterating and have to change one property of the object. I have two scenarios,...
Read more >
The Spread Operator: Deep and Shallow Copies. | by Kevin Lai
The spread operator makes deep copies of data if the data is not nested. When you have nested data in an array or...
Read more >
Tip 8: Avoid Push Mutations with the Spread Operator
In this tip, you'll learn how to avoid array mutations by creating new arrays with the spread operator. We'll cover the following. Avoiding...
Read more >
Spread Operator in JavaScript - Stack Abuse
As we learned, the spread operator expands the contents of an iterable. In contrast, the rest operator collects all the remaining elements into ......
Read more >
Rest parameters and spread syntax
It depends on the context. The example provided here focuses on spread being used in an array context. If you did [...o1], it...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found