Skip to content
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

Realm object constructor must not return another value #2790

Closed
mariasiuvlad opened this issue Apr 1, 2020 · 10 comments
Closed

Realm object constructor must not return another value #2790

mariasiuvlad opened this issue Apr 1, 2020 · 10 comments
Assignees

Comments

@mariasiuvlad
Copy link

mariasiuvlad commented Apr 1, 2020

Goals

Hi, my goal is to use ES6 Classes and inheritance as Realm models.
This possibly relates to #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

#2768 (comment) 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 #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

@hbajji
Copy link

hbajji commented Apr 3, 2020

I got the same error

"react": "16.8.3",
"react-native": "0.59.9",
"realm": "^2.25.0",
node v10.15.2
macOS Majova 10.14.6

@blagoev
Copy link
Contributor

blagoev commented Apr 6, 2020

Update Realm to the latest version (v5.0.3) which brings proper support to ES6 class syntax.
Right now we don't require the class to extend Realm.Object. In the future we will be changing that to be required. In either case Realm v5 should be working for your case

@mariasiuvlad
Copy link
Author

mariasiuvlad commented Apr 6, 2020

@blagoev Thank you for your reply. Using 5.0.3, the issue still comes up, though only when using debug mode.

Maybe this is due to the fact that debug mode uses a different runtime? https://reactnative.dev/docs/javascript-environment#javascript-runtime

@blagoev
Copy link
Contributor

blagoev commented Apr 8, 2020

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

@mariasiuvlad
Copy link
Author

@blagoev Thank you for the info. Do you have a timeframe for this fix by any chance? It would be very helpful.

@kneth
Copy link
Contributor

kneth commented Jun 25, 2020

See also #2799

@nikkundra
Copy link

Hi @blagoev -- Just wanted to add a request here as well. Do you have a timeframe for this fix? 🙏🏽

@RealmBot
Copy link
Collaborator

RealmBot commented Oct 8, 2020

➤ Lyubomir Blagoev commented:

Please update to latest 6.1.3. Also change the schema classes to inherit from Realm.Object

class Person extends Realm.Object {

}

@sync-by-unito sync-by-unito bot closed this as completed Nov 2, 2020
@sync-by-unito
Copy link

sync-by-unito bot commented Nov 2, 2020

➤ Lyubomir Blagoev commented:

closing this issue as stale

@aqos156
Copy link

aqos156 commented 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

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Mar 15, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

7 participants