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.

Spreading a Realm.Object gives in an empty object

See original GitHub issue

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 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:open
  • Created 4 years ago
  • Reactions:10
  • Comments:11 (1 by maintainers)

github_iconTop GitHub Comments

7reactions
hossein-zarecommented, Jan 4, 2021

This works for me.

let data = realm.objectForPrimaryKey('User', 10);

data = JSON.parse(JSON.stringify(data));

console.log({...data, name: 'Bob'});
1reaction
Waltari10commented, Apr 29, 2022

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)

Read more comments on GitHub >

github_iconTop 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 >

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