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)

Most upvoted comments

@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 .schema to 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.

  • If my classes extend Realm.Object, I get Reflect.construct errors in release builds or Realm object constructor must not return another value errors in development
  • If I don’t extend Realm.Object then my realm.deletes are failing

I’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

TypeError: Reflect.construct requires the first argument be a constructor
construct@[native code]
http://10.0.2.2:8081/index.bundle?platform=android&dev=true&minify=false:131712:35
TestSchema@http://10.0.2.2:8081/index.bundle?platform=android&dev=true&minify=false:131742:26
create@[native code]

It happens when calling realm.create. TheSchema also extends Realm.Object.

The code causing this in bundle.js looks like this:

  function _createSuper(Derived) {
    function isNativeReflectConstruct() {
      if (typeof Reflect === "undefined" || !Reflect.construct) return false;
      if (Reflect.construct.sham) return false;
      if (typeof Proxy === "function") return true;

      try {
        Date.prototype.toString.call(Reflect.construct(Date, [], function () {}));
        return true;
      } catch (e) {
        return false;
      }
    }

    return function () {
      var Super = (0, _getPrototypeOf2.default)(Derived),
          result;

      if (isNativeReflectConstruct()) {
        var NewTarget = (0, _getPrototypeOf2.default)(this).constructor;
        result = Reflect.construct(Super, arguments, NewTarget);
      } else {
        result = Super.apply(this, arguments);
      }

      return (0, _possibleConstructorReturn2.default)(this, result);
    };

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.construct call in bundle.js

EDIT 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.