Realm-js: [5.0.3]realm.delete can NOT delete the object returned by realm.objectForPrimaryKey

Created on 16 Apr 2020  路  6Comments  路  Source: realm/realm-js

Goals

delete an object from realm by id like this
realm.write(()=>{ realm.delete(realm.objectForPrimaryKey(modelName, id)); })
It works fine all the time until I upgrade realm from 2.29.2 to 5.0.3.

Expected Results

successfully delete realm object by id

Actual Results

failed to delete, an error occurred:

Argument to 'delete' must be a Realm object or a collection of Realm objects.

I think realm.delete has failed to recognize the object returned by objectForPrimaryKey as an realm object, but I don't know why.

Code Sample

realm.write(()=>{ realm.delete(realm.objectForPrimaryKey(modelName, id)); })

Version of Realm and Tooling

  • Realm JS SDK Version: 5.0.3
  • Node or React Native: react-native 0.60.5
  • Client OS & Version: macOS 10.15.4
  • Which debugger for React Native: ?/None
O-Community

Most helpful comment

I'm getting this error as well.

I'm not sure if it's related, but there might be something fundamentally wrong with my realm config. All of the objects in any sort of Realm.Results or Realm.Collection appear to be empty when I print them (literally {}), but the properties correctly resolve when accessed.

For instance:

const pool = realm.objectForPrimaryKey('Pool', objectId);
console.log(pool);
console.log(pool.name);

This will print:

{}
examplePoolName

All 6 comments

thanks for reporting. we will investigate this further.

@YaoHuiJi did you had the RN app with debugger enabled or this happens regardless?

Also just to double check are you sure you are getting an actual Realm object from realm.objectForPrimaryKey(modelName, id). Or it's null or undefined ?

hi @blagoev,
I tried just now and found out it works well when debugger is enabled ,both on simulator and real device.
and I have log the output of realm.objectForPrimaryKey, it's not null or undefined, and obj.isValid() is true.
I also tried getting an object via realm.objects and delete it, failed too, same error.
if you need more information, please tell me.

I'm getting this error as well.

I'm not sure if it's related, but there might be something fundamentally wrong with my realm config. All of the objects in any sort of Realm.Results or Realm.Collection appear to be empty when I print them (literally {}), but the properties correctly resolve when accessed.

For instance:

const pool = realm.objectForPrimaryKey('Pool', objectId);
console.log(pool);
console.log(pool.name);

This will print:

{}
examplePoolName

All of my issues disappear if I pass the schemas directly, instead of the classes, when calling Realm.open():

https://github.com/realm/realm-js/issues/2848#issuecomment-631485221

[Edit: technically, _all_ of my issues don't disappear. I've still got a mortgage, a toothache, etc... but, Realm does return better-looking objects, and I can even delete them.]

6.0.4 solved this issue, thank you all.

Was this page helpful?
0 / 5 - 0 ratings