-
Notifications
You must be signed in to change notification settings - Fork 577
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Spreading a Realm.Object gives in an empty object #2640
Comments
Spread operator is very tricky and makes confusing about behaviour. It doesn't copy getters/setters, just values: realm-js/lib/browser/objects.js Line 59 in cd57977
Anyway. |
@radeno thanks for your interest. To me the spread operator is pretty straight forward - it makes sense to me that it doesn't copy getters/setters, but this is also not what this issue is about. Thanks for the hint on the use of I believe the root cause of this issue is that both spread and Object.assign() only consider own enumerable properties: Object.getOwnPropertyDescriptors(alice)
/* 👇
{ _realm:
{ value: Realm {},
writable: true,
enumerable: false,
configurable: false },
name:
{ value: 'Alice',
writable: true,
enumerable: false,
configurable: true } }
*/ |
Same issue here, tested on RN I'trying to load an Object to a const, that I'm using with the useState hook, that's because i can change the full object fields with just one useState setter callback I've try the JSON.parse(JSON.stringify(testobj)) trick, but I've no luck with dates that became strings, and also it's not that beauty. thank you! |
For those who are interested in, I've found a quick fix here, I'm testing it (mapProps method) #141 (comment) |
Even i'm facing the same issue where hasOwnProperty throw false. |
Created an alternative solution in typescript. Haven't tried using shallow clone, but deep clone seems to do the job. Obviously you can implement your own cloneDeep function, but lodash is used here out of pure laziness.
|
This works for me. let data = realm.objectForPrimaryKey('User', 10);
data = JSON.parse(JSON.stringify(data));
console.log({...data, name: 'Bob'}); |
We found lodash functions such as orderBy and isEmpty not working with realmObject anymore which worked well with the legacy realm version. |
import Realm from 'realm'
export function shallowCloneRealmObject<T extends Realm.Object>(x: T) {
const copy: any = {}
for (const key in x) {
copy[key] = x[key]
}
return copy as Exclude<T, Realm.Object>
} My workaround for now:
|
If people are interested I published to npm an eslint-plugin that bans spread on Realm.Object type. (hopefully spread operator wont be fixed now the next day) |
Note that |
Version 12.0.0 introduced support for the spread operator |
Goals
I would like to be able to easily convert a
Realm.Object
to a plain-ol'-javascript-object.Expected Results
When doing a JS spread on a
Realm.Object
I would expect the resulting object contained the same properties as theRealm.Object
.Actual Results
You get an empty object.
Steps to Reproduce & Code Sample
An interesting observation is that
getOwnPropertyNames
contains'name'
butObject.hasOwnProperty(alice, 'name')
is false ♂️Version of Realm and Tooling
The text was updated successfully, but these errors were encountered: