Spreading a Realm.Object gives in an empty object
See original GitHub issueGoals
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 the Realm.Object
.
Actual Results
You get an empty object.
Steps to Reproduce & Code Sample
const Realm = require('realm')
const realm = new Realm({ schema: [ { name: "Person", properties: { name: "string" } } ] });
realm.write(() => { realm.create("Person", { name: "Alice" }) });
const persons = realm.objects("Person"); // 👉Results { [0]: RealmObject { [name]: 'Alice' } }
const alice = persons[0]; // 👉RealmObject { [name]: 'Alice' }
Object.keys(alice) // 👉[ 'name' ]
Object.getOwnPropertyNames(alice) // 👉[ '_realm', 'name' ]
alice.hasOwnProperty('name') // 👉true
Object.getOwnPropertyDescriptors(alice)
/* 👇
{ _realm:
{ value: Realm {},
writable: true,
enumerable: false,
configurable: false },
name:
{ value: 'Alice',
writable: true,
enumerable: false,
configurable: true } }
*/
{ ...alice } // 👉{}
An interesting observation is that getOwnPropertyNames
contains 'name'
but Object.hasOwnProperty(alice, 'name')
is false 🤷♂️
Version of Realm and Tooling
- Realm JS SDK Version: 3.5.0
- Node or React Native: Node v10.17.0
- Client OS & Version: N/A
- Which debugger for React Native: None
Issue Analytics
- State:
- Created 4 years ago
- Reactions:10
- Comments:11 (1 by maintainers)
Top Results From Across the Web
Realm.objects(Type) return empty objects - Stack Overflow
I'm a new in Xcode development and I need some help with Realm database .. these are my tables
Read more >New Realm.App({ id: appId }) produces an empty object
Hi everyone! I'm using Realm for the first time for a React Native (created via react-native init) project, and I'm a bit confused...
Read more >ECMAScript® 2023 Language Specification - TC39
4.3.1 Objects; 4.3.2 The Strict Variant of ECMAScript ... ◢14.4 Empty Statement ... through inheritance, by all objects sharing the prototype.
Read more >Class: Collection - Realm
Returns: [Realm.Object, ...] containing the objects from the start index up to, but not including, the end index. ... Create a frozen snapshot...
Read more >Realm with SwiftUI - Create, Read, Update, Delete ... - YouTube
In this video, Mohammad Azam will demonstrate how to use Realm framework in a SwiftUI application to persist data into the database.
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
This works for me.
If people are interested I published to npm an eslint-plugin that bans spread on Realm.Object type. https://www.npmjs.com/package/@waltari/eslint-plugin-ban-realm-spread
(hopefully spread operator wont be fixed now the next day)