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.

Realm object constructor must not return another value

See original GitHub issue

Goals

Hi, my goal is to use ES6 Classes and inheritance as Realm models. This possibly relates to https://github.com/realm/realm-js/issues/2768 After upgrading realm and react-native from

"realm": "2.23.0",
"react": "16.0.0",
"react-native": "0.50.3",

the following error shows up Realm object constructor must not return another value

I was wondering if there is any way to get this to work with later versions

https://github.com/realm/realm-js/issues/2768#issuecomment-604438625 This mentions that we shouldn’t extend Realm.Object but doesn’t say anything else about extending other classes. https://realm.io/docs/javascript/latest/#classes The docs don’t mention anything about this either.

Code Sample

class BaseModel {}
 
export class Account extends BaseModel {
  static schema = {
    name: 'Account',
    primaryKey: 'uuid',
    properties: {
      uuid: 'string',
      id: {type: 'int', indexed: true},
      firstName: 'string',
      lastName: 'string',
      username: 'string',
      apiKey: 'string',
      lastLogin: 'date'
    },
  };
}

database = new Realm({
      path: 'core.realm',
      schema: [
        Account, // eslint-disable-line no-use-before-define
        // ...
      ],
      schemaVersion: 13,
      migration: () => null,
    });

Similar to https://github.com/realm/realm-js/issues/2768 the error goes away when removing the extends

Version of Realm and Tooling

"realm": "^3.6.5",
"react": "^16.13.1",
"react-native": "0.62.0",
node v10.14.2
macOS Catalina 10.15.4

I also tried 5.0.2 and 4.0.0-beta.2 and got the same error

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:10 (3 by maintainers)

github_iconTop GitHub Comments

4reactions
blagoevcommented, Apr 8, 2020

We have a bug in the RN debugging infrastructure for v5 and we are working to fixing this.

1reaction
aqos156commented, Apr 2, 2021

This error seems to be still happening in 10.3.0 - we have just migrated our code to be compatible with the version 10.x.x from 3.x.x and are facing this issue. If we do not use a debugger (RN Debugger, Google Chrome debugger) it works as expected.

Model

export class User extends Realm.Object {
  static REGISTERED: number = 0;
  static GUEST: number = 1;
  static NOT_ACTIVATED: number = 2;

  static schema = {
    name: "User",
    primaryKey: "id",
    properties: {
      id: "int",
      etc...
    }
}

Accessing an object instance

const users = realm.objects("User");
console.log("Loaded", users[0]);

lib/browser/objects.js (the error is thrown here)

Snímek obrazovky 2021-04-02 v 15 12 47

Console output

Snímek obrazovky 2021-04-02 v 15 12 06

Error

Snímek obrazovky 2021-04-02 v 15 18 32

Anybody knows the reason (or a fix)?

I created an issue from this comment #3673

Read more comments on GitHub >

github_iconTop Results From Across the Web

Realm object constructor must not return another value
I just want to report an issue, please advice me for best practice. Code Sample. I have codes below: try { const upsert...
Read more >
[Realm/React-Native debugger]: constructor must be of ...
But on debugger mode, I am getting error when try to create Realm object with schema parameters. import Realm from 'realm'; class Todo...
Read more >
Realm: Create reactive mobile apps in a fraction of the time
When performing queries you are returned a Results object. Results are simply a view of your data and are not mutable. The most...
Read more >
The things I've learned using Realm | by Gabor Varadi - Medium
In this article, I try to sum up all the things I've learned from using Realm, and how it all relates with the...
Read more >
Realm Database on Android: Getting Started
A Realm object reads and writes data to and from the database. ... You'll have to trust me on this, because the app...
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