realm / realm-js

Realm is a mobile database: an alternative to SQLite & key-value stores
https://realm.io
Apache License 2.0
5.62k stars 558 forks source link

[5.0.0] Android returns Results with length 1 but no keys or values #2763

Closed bimusiek closed 4 years ago

bimusiek commented 4 years ago

Goals

I am trying to fetch some objects from database.

Expected Results

Getting 1 object with all properties.

Actual Results

I get Results of length 1 but when getting the object via index 0, it returns no properties.

Steps to Reproduce

Run the code on Android

Code Sample

const results = realm
        .objects<ImporterAuthenticationModel>(ImporterAuthenticationModel.schema.name)
        .filtered('importerId = $0', importer.importer.id);

      if (results) {
        console.log(results.length);
        console.log(results[0].id);
      }

Output is:

1
undefined

Schema:

export class ImporterAuthenticationModel extends BaseModel {
  public static schema = {
    ...BaseModel.schema,
    name: 'ImporterAuthenticationModel',
    primaryKey: 'uniqueId',
    properties: {
      ...BaseModel.schema.properties,
      uniqueId: 'string',
      id: 'string',
      title: 'string',
      subTitle: 'string?',
      imageUrl: 'string?',
      importerId: 'string',
      additionalDataString: 'string?',
      rawStatus: { type: 'int', default: ImporterAuthenticationStatus.AUTHENTICATED },
      expiresAt: 'int'
    }
  };

Version of Realm and Tooling

bimusiek commented 4 years ago

I've checked on other device, same results. Adding new realm models to database work and everything is synced to Realm Cloud.

But fetching anything from Realm yields empty objects.

I have checked on Realm 3.6.5 & Node 10 and it works. So my guess is that due to this change: Realm objects properties are now defined as accessors on the instance prototype. Previously they were defined as values on the instance. Android no longer returns properties of an object.

florigo commented 4 years ago

Same here, Realm 5.0.2 and Node 10.19. The issue appeared only when I rebuilt and installed the apk package, with the new Realm version. Not clear if it stores the objects correctly or not. I synchronously opened Realm for this task.

steffenagger commented 4 years ago

I'm seeing the same issue in Realm 5.0.2. @florigo when creating new objects, they find their way correctly all the way to Realm Cloud, via Sync, with all values intact - so the data should be stored correctly. @bimusiek's assumption, that the issue is with populating local models, seems correct.

As an extra note, this is not an issue for the latest beta 4.0.0-beta.2, which is targeted for 6.0.0.

bimusiek commented 4 years ago

@steffenagger My guess is that 4.0.0-beta.2 works because it does not have new NAPI implementation merged yet (does not work with Node 13 whereas 5.0.x does).

We had to make our react-native apps use 3.6.5 version where Electron app uses 5.0.1. Seems sketchy to have different versions but it works.

Mavenic commented 4 years ago

Facing the same issue as well but I am using react-native 61.5, node 10.15, realm 5.0.2

kraenhansen commented 4 years ago

@Mavenic and @florigo: I just tried, but failed to reproduce this running the code in Node.js v10.19.0 and v12.14.1, please create your own issue with exact instructions on how to reproduce this.

kraenhansen commented 4 years ago

@bimusiek how are you creating the Realm instance? Specifically, are you passing the ImporterAuthenticationModel class or the static schema ImporterAuthenticationModel.schema property into the schema?

bimusiek commented 4 years ago

@kraenhansen I am passing ImporterAuthenticationModel.schema to realm.

  const user = await getRealmUser();
  const realmConfig = user.createConfiguration({
    schema,
    schemaVersion,
    sync: {
      url: `~/app`,
      fullSynchronization: true
    }
  });
  const realm = new Realm(realmConfig);

where schema & schemaVersion are:

export const schema = [
  ConfigRealmModel.schema,
  CollectionModel.schema,
  CollectionItemModel.schema,
  ImporterAuthenticationModel.schema,
  TransferModel.schema,
  TransferCollectionModel.schema,
  TransferCollectionItemModel.schema,
  LicenseRequestModel.schema
];
export const schemaVersion = 1;

The same code works on both 3.6.5 version and 4.0.0-beta2. 5.0.x works on iOS + Electron + Node Server but it does not return any keys and values on Android. Creating & updating models work though, and it is synced to the Cloud.

I tried bumping JSC to newest version, it did not help.

kraenhansen commented 4 years ago

I can confirm the issue on with React Native 0.61.5 and Realm 5.0.2 on Android running in an emulator. Thanks for reporting this @bimusiek!

cristianoccazinsp commented 4 years ago

Same issue. RealmJS 5.0.2 (migrated from 3.x) is basically unusable on Android, all results are empty, and getting a bunch of errors here and there (failed deletes, empty objects, etc.).

Any work around? Or should we just revert to 3.x until this is resolved?

kraenhansen commented 4 years ago

I am working on getting this fixed as-we-speak, if you're not relying on the Node 12 + 13 support, we recommend reverting to 3.x until this issue is fixed. We'll be updating our latest tag on NPM to point to 3.x until we find a solution for the 5.x release.

kraenhansen commented 4 years ago

I'm having the same issue on 5.0.2 on iOS (Simulator)

@matt2legit I've tried but I've been unsuccessful at reproducing this issue on iOS. I would really appreciate if you could create an issue, separate from this, with details on the version of device/simulator, React Native and if possible a short piece of code / link to a small repository demonstrating the issue.

saravanakumargn commented 4 years ago

I am facing the same issue. Android its not working. iOS Working.

    "realm": "^5.0.2",

This is the results i am getting in Android Device.

{"0": {}, "1": {}, "2": {}, "3": {}, "4": {}, "5": {}, "6": {}, "7": {}, "8": {}}

iOS (Simulator) it's working as expected Realm without any issues..

Alphia commented 4 years ago

I am facing the same issue. Android its not working. iOS Working.

    "realm": "^5.0.2",

This is the results i am getting in Android Device.

{"0": {}, "1": {}, "2": {}, "3": {}, "4": {}, "5": {}, "6": {}, "7": {}, "8": {}}

iOS (Simulator) it's working as expected Realm without any issues..

Get the same error.

gys-dev commented 4 years ago

I am facing the same issue. Android its not working. iOS Working.

    "realm": "^5.0.2",

This is the results i am getting in Android Device.

{"0": {}, "1": {}, "2": {}, "3": {}, "4": {}, "5": {}, "6": {}, "7": {}, "8": {}}

iOS (Simulator) it's working as expected Realm without any issues..

+1

blagoev commented 4 years ago

Realm JS 5.0.3 is released fixing this. Closing this issue. If it is not fixed for someone for some unexpected reason, just reopen it.

MrScorp commented 4 years ago

Checked out 5.0.3 with React Native 0.61.5. Doesn't work in iOS, works in android though!!

const testSchema = { name: "testdata", properties: { name: "string" }}; const realm = await Realm.open({schema: testSchema}); const data = { name : "TEST" };

realm.write(() => { realm.create("testdata", data); });

console.log(realm.objects("testdata"));

Result in iOS: { '0': {}}

Result in Android: {"0": {"name": "TEST"}}

EDIT : As @blagoev noted below, "realm.objects" call is returning the data which was pushed in to the DB. Somehow, it isn't showing up in the debugger output.

blagoev commented 4 years ago

@MrScorp did you used a debugger when checking that. Could you try without attaching a debugger

MrScorp commented 4 years ago

Oh yes, tried without the debugger, and IT WORKS !

Thanks a Ton @blagoev (y)

I'm intrigued.. Whats this enmity with the debugger though? :)

cristianocca commented 4 years ago

Does this mean we can no longer debug apps that use Realm?

blagoev commented 4 years ago

@cristianocca We have a bug in the RN debugger for Chrome and are working to fix it. You can follow it here https://github.com/realm/realm-js/issues/2799. Until then you can use the WebKit debugger to debug RN apps with Realm.

bimusiek commented 4 years ago

I can confirm it works in Android and iOS 👍

vishaljadav24 commented 4 years ago

@bimusiek I am getting results with no keys and values in Android. Though I am creating realm instance by synchronous way( const realm = new Realm({schema : [Table1, Table2]) ). Is this causing the problem?

Library versions: realm: 5.0.3 react-native: 0.62.2

vishaljadav24 commented 4 years ago

@bimusiek I have also tested by create realm instance in asynchronous way(Realm.open) but it's also returning same results.

Codder-RN commented 3 years ago

I'm trying to debug with Reactotron, Fllipper, with let allCliente = realmschema.objects('Cliente'); and I get the following result 0:{},1:{},

libraries: "react-native": "0.63.0-rc.1", "realm": "^6.0.1",

kneth commented 3 years ago

@BdN0 We have not Flipper support which might explain what you see.

Codder-RN commented 3 years ago

@BdN0 We have not Flipper support which might explain what you see.

But what about the Reactotron? before there was support for him

image

libraries: "react-native": "0.63.0-rc.1", "realm": "^6.0.1",

Jarzka commented 3 years ago

Seems that this issue should have been fixed in 5.0.3. However, I'm now running 5.0.5 and facing this issue: All realm.objects queries return empty objects. Also, I do not have React Native Debugger attached and I'm not using Flipper.

Any ideas how to fix it? Did you solve it @vishaljadav24 ?

EDIT: It seems that this empty object issue can be "solved" by downgrading back to 3.6.5.

ghost commented 3 years ago

Hi @kraenhansen I am getting the issue on both IOS and android Versions -> "react-native": "0.62.2", "realm": "^6.0.1",

ghost commented 3 years ago

Hi @blagoev @kraenhansen Any update on this issue ?. Can anyone reopen this issue ?

keima commented 3 years ago

Same problem is here. But our project uses realm.objects() methods everywhere and some method call is fine but others not working.

Upon investigation, we found that it worked fine for classes that inherited from Realm.Object, but returned an empty object for classes that didn't. This is odd.

Working correctly:

export class RealmPlayLog extends Realm.Object {
  static readonly schema: ObjectSchema = schema

  ...
}

Not working correctly:

export class RealmPlayLog {
  static readonly schema: ObjectSchema = schema

  ...
}

"realm": "^6.0.1" (yarn.lock says 6.0.1) "react-native": "0.61.5",

iOS Simulator: iPhone 11 (13.1)

samuelrvg commented 3 years ago

@keima It worked, thank you!

machadolucasvp commented 3 years ago

Currently facing this issue with 5.0.4 and without debugger. realm.objects return empty object and the code base use it everywhere...

"react-native": "0.61.5" "realm": "5.0.4" running in android