Hi, i took a completely new init, and compared a JSON.stringify of a class vs non class.
It appears that a class dosen't seem to show the properties of the schema. Not sure if its expected.
You will realize the code is exactly the same as the docs. (using the person class example and the dog json example)
And you can see the dog works but the person does not

"react-native": "0.63.2",
"realm": "^6.0.4"
class Person {
get fullName() {
return this.firstName + ' ' + this.lastName;
}
}
Person.schema = {
name: 'Person',
properties: {
firstName: 'string',
lastName: 'string',
},
};
const App: () => React$Node = () => {
const [realm, setRealm] = useState(null)
useEffect (()=>{
Realm.open({
schema: [
{
name: 'Dog', properties: {name: 'string'}
}, Person
]
}).then(realm => {
realm.write(() => {
realm.create('Dog', {name: 'Rex'});
const john = realm.create('Person', {
firstName: 'John',
lastName: 'Smith',
});
});
let dogs = realm.objects('Dog');
let persons = realm.objects('Person');
for (let d of dogs) console.log("dog ", JSON.stringify(d))
for (let p of persons) console.log("person ", JSON.stringify(p))
setRealm({ realm });
});
}, [])
@enigmablue what happens if you let Person extend from Realm.Object, like so:
class Person extends Realm.Object {
get fullName() {
return this.firstName + ' ' + this.lastName;
}
}
Person.schema = {
name: 'Person',
properties: {
firstName: 'string',
lastName: 'string',
},
};
hm i got this error Realm TypeError: Reflect.construct requires the first argument be a constructor
If I put the schema as Person.schema it works.
Realm.open({
schema: [
Person.schema
] })
But now the .fullName is undefined. And am trying to figure out how to setup proper methods in that class.
If I try to call .fullName() it says 'TypeError: p.fullName is not a function'. If I call fullName, it is undefined.
Got the exact same issue. Also get the same error when trying the suggested solution.
The issue arose when upgrading from realm 3.3.0 to 6.0.5.
Any solutions?
Having same issue, issue appeared when upgraded to 6.1.0. Also it looks like Documentation is not up to date, as part of extending object, is missing there.
I can confirm that I'm also seeing the Reflect.construct requires the first argument be a constructor error when extending Realm.Object in RN, and will be investigating how this slipped through our tests.
The error arises as soon as a class model extends Realm.Object. My apologies for guiding in that direction.
A _temporary_ solution for getting serialization working on class models would be (_instead_ of extending Realm.Object) to implement toJSON on your class models like so:
class Person {
firstName;
lastName;
get fullName() {
return this.firstName + ' ' + this.lastName;
}
toJSON() {
return Realm.Object.prototype.toJSON.call(this);
}
}
This will still not include fullName, as JavaScript per design does not include getters in these operations.
You can though opt in by manually extending the object for serialization:
toJSON() {
return {
fullName: this.fullName,
...Realm.Object.prototype.toJSON.call(this),
};
}
i think the problem is a lot of people (including myself) are currently having code that extends Realm.object
So we can't upgrade, and I dont know if I want to change the type of class... as I remember there were other issues... maybe i'd have to try again...
I currently also have static methods in the class.. so I can do stuff like Person.getById(), Person.doSomething() , may have to check that.
My current class..
class Share extends Realm.Object {
static getShares(ids) {
if (!ids || ids.length < 1) {
return;
}
try {
let realmResult = realm
.objects(SHARE_SCHEMA)
.filtered(ids.map(_id => '_id == ' + `'${_id}'`).join(' OR '));
return realmResult;
} catch (e) {
handleError(e, 'getShares');
}
}
static schema = {
name: SHARE_SCHEMA,
primaryKey: '_id',
properties: {
_id: {
type: 'string',
optional: true,
},
tag: {
type: 'string',
optional: true,
},
};
}
@enigmablue completely understandable, we're working on this issue.
Will post here when we have something concrete.
@enigmablue completely understandable, we're working on this issue.
Will post here when we have something concrete.
Thanks so much! Was stuck on this for a while, was wondering why no one else had this issue haha.
I think many has worked around this issue by passing in Person.schema instead of Person, when initiating the Realm. But this neglects getting the actual class models when fetching - so again, not really a solution I can recommend.
Closing this as a duplicate of https://github.com/realm/realm-js/issues/3110 - please watch that issue to get notified when we get this fixed.
Most helpful comment
I can confirm that I'm also seeing the
Reflect.construct requires the first argument be a constructorerror when extendingRealm.Objectin RN, and will be investigating how this slipped through our tests.The error arises as soon as a class model extends
Realm.Object. My apologies for guiding in that direction.A _temporary_ solution for getting serialization working on class models would be (_instead_ of extending
Realm.Object) to implementtoJSONon your class models like so:This will still not include
fullName, as JavaScript per design does not include getters in these operations.You can though opt in by manually extending the object for serialization: