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.
successfully delete realm object by id
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.
realm.write(()=>{
realm.delete(realm.objectForPrimaryKey(modelName, id));
})
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.
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.ResultsorRealm.Collectionappear to be empty when I print them (literally{}), but the properties correctly resolve when accessed.For instance:
This will print: