Confusion due to (proposed) spread operator on model instance
See original GitHub issueSee 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:
- Created 7 years ago
- Reactions:4
- Comments:8
Top 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 >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
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 usecard.set(cardData)
, seeDocument#set()
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