realm-js: Realm.delete is not working with class schema
Goals
Realm.delete works on older versions , After upgrading from 3.4.1 to 5.0.3 , Realm.delete is not working with class schema
Actual Results
Argument to 'delete' must be a Realm object or a collection of Realm objects.
Steps to Reproduce
class Person {
get fullName() {
return this.firstName + ' ' + this.lastName;
}
}
Person.schema = {
name: 'Person',
properties: {
firstName: 'string',
lastName: 'string'
}
};
Realm.open({schema: [Person]})
.then(()=>{
realm.write(() => {
const john = realm.create('Person', {
firstName: 'John',
lastName: 'Smith'
});
john.lastName = 'Peterson';
realm.delete(john)
});
});
Code Sample
Version of Realm and Tooling
- Realm JS SDK Version: 5.0.3
- Node or React Native: 10.15.3 - 0.62.2
- Client OS & Version: ? Windows 10 10.0.18362
- Which debugger for React Native: None
About this issue
- Original URL
- State: closed
- Created 4 years ago
- Reactions: 1
- Comments: 22 (3 by maintainers)
@bkbooth I’ve found a workaround for now. @blagoev mentioned this issue is specific to class-based schemas, so the workaround is to pass the schemas directly.
The change was actually pretty small for me. When I call
realm.open(), I changed the config from:{ schema: [Pool, LogEntry, ReadingEntry, TreatmentEntry], schemaVersion: 0 }to
{ schema: [Pool.schema, LogEntry.schema, ReadingEntry.schema, TreatmentEntry.schema], schemaVersion: 0 }So, I just appended
.schemato each class in the array. This works if your schemas are defined statically 😄I’m desperately needing a solution for this too. My app seems stuck in a broken state.
extend Realm.Object, I getReflect.constructerrors in release builds orRealm object constructor must not return another valueerrors in developmentextend Realm.Objectthen myrealm.deletes are failingI’d be happy for temporary hacks or workarounds at this point.
We have fixed the realm.delete error. It will be included in our next release. Thanks for reporting it.
I also get the same error
It happens when calling
realm.create. TheSchema also extends Realm.Object.The code causing this in bundle.js looks like this:
This started to happen after I updated react-native 0.59 → 0.62.2 and Realm 2.29.2 → 5.0.4.
EDIT:
It seems that this happens even if I revert Realm back to 2.29.2, but still keep react-native 0.62.2 and some other libraries updated.
When running old react-native 0.59 and Realm 2.29.2, there is no
Reflect.constructcall in bundle.jsEDIT 2:
If I’m correct, the code pasted above (Reflect.construct) is not related to Realm, but is generated from Babel. However, it also did not help even if I downgraded Babel.
Ah, no instance methods for me. I keep my models dumb.