realm-js: Realm returns blank when not using React Native Debugger

Goals

I’m trying to add an entity into Realm and then read and list it in another scene.

Expected Results

The data that is read is supposed to be rendered as a list. It does happen but the data itself is not present.

Actual Results

The data is saved to the database and is subsequently read from the database. A single item is rendered to the list, but all its values are empty. When clicking the item, a new screen opens which showcases all the data from that specific item (the item itself only shows 2 of the details) This only happens if I’m not running the React Native debugger. I’ve generated the debug APK for testing purposes and the error still persisted.

Steps to Reproduce

I’m not sure what can be done to reproduce this. I’m saving one item to a schema and later reading it. The data is retrieved but all the values in it are empty.

Code Sample

1.This is the code that reads the data from the database. None of the values are undefined or null. This return the blank data when not using the debugger:

const data = realm.objects('Visita').filtered(`matricula == '${this.props.matricula}' AND idObra == ${this.props.data.id}`)
  1. This is the code that created the data prior to it being read in another page:
 getRealm()
                .then(realm => {
                    /*Some boilerplatecode*/
                    realm.write(() => {
                        realm.create('Visita', visita)
                    })
  1. This is the schema that is being added:
export default class VisitaSchema {
    static schema = {
        name: 'Visita',
        primaryKey: 'idVisita',
        properties: {
            idVisita: { type: 'int', indexed: true },
            descricao: 'string',
            latitude: 'string',
            longitude: 'string',
            matricula: 'string',
            idObra: 'int',
            data: 'string'
        }
    }
}

Version of Realm and Tooling

  • Realm JS SDK Version: ?
  • Node or React Native: ?
  • Client OS & Version: ?
  • Which debugger for React Native: ?/None

About this issue

  • Original URL
  • State: closed
  • Created 5 years ago
  • Reactions: 4
  • Comments: 38 (11 by maintainers)

Most upvoted comments

After a day of trying to get realm to work with a fresh react-native app, we’ve abandoned all hope. Best fix for this 2 year old issue is:

yarn remove realm

@manoj-makkuboy if you are using class models, please try extending from Realm.Object, like so:

class MyModel extends Realm.Object {
  ...
}

So I had the same issue as well. I was able to resolve it by following these steps:

  1. Upgrade the realm package to 5.0.3 yarn upgrade realm

  2. Run npx react-native link realm (Realm does not fully support auto-linking yet)

  3. Verify that the linking happened properly by following the instructions at the top of: https://realm.io/docs/javascript/latest/

  4. Make sure to add this line --> import io.realm.react.RealmReactPackage; to the top of your MainApplication.java in android/app/src/main/java/com/<your-app-name>/MainApplication.java

  5. Check your package list in android/app/src/main/java/com/<your-app-name>/MainApplication.java to ensure it is configured as followed:

        @Override
        protected List<ReactPackage> getPackages() {
          @SuppressWarnings("UnnecessaryLocalVariable")
          List<ReactPackage> packages = new PackageList(this).getPackages();
          // Packages that cannot be autolinked yet can be added manually here, for example:
          // packages.add(new MyReactNativePackage());
+         packages.add(new RealmReactPackage()); // <-- This line adds in the realm package.
          packages.add(new RNSensitiveInfoPackage());
          return packages;
        }
  1. cd into /android directory and clean by running ./gradlew clean

  2. Re-compile and run again from your project’s root directory npx react-native run-android

** If you get an error that looks something like this:

Native module realm tried to override realm for module name realm. If this was your intention, set canOverrideExistingModule=true
  1. Open <your-app-name>/node_modules/realm/android/src/main/java/io/realm/react/RealmReactModule.java

  2. Add the following lines inside the RealmReactModule class:

    @Override
    public boolean canOverrideExistingModule() {
        return true;
    }

Hope this helps! 🙂

this issue is still remain, I’m using Objects.find as a workaround.

realm.objects(tablename).find(({ID}) => ID === paramID)

I’m not using any debugger at all and experiencing the same issue. @samuelrvg extends from Realm.Object gives me Reflect.construct requires the first argument be a constructor running in a android with react-native:0.61.5, using dumb schemas resolve the problem with empty objects in 5.0.4. This issue still reproducible for me in 6.0.4, someone can take a look at this ?

Same issue here also guys! Thank you @havi-b and @blagoev for this workaround, but it didn’t work for me. So I have to downgrade my Realm to version 3.6.5 (npm i --save realm@3.6.5 or yarn add realm@3.6.5). After those objects() and objectForPrimaryKey() functions return to work fine.

I think this 5.x.x and 4.x.x (beta) versions are broken for React Native!

@havi-b Thanks for the description. One note: you should be able to do it without changing the getPackages method so this line is not needed or shouldn’t be needed now packages.add(new RealmReactPackage()); // <-- This line adds in the realm package.

I have a same issue with latest realm version and RN 0.64 Very sad that no one really care about that bug and unfortunately force us to use different local db.

Unfortunately can confirm that we are still facing the same issue even in the newest version of realm. We have to still use the realm version 3

@manoj-makkuboy in realm@6.1.0 you should be able to use objRealm.toJSON() to get at plain JS structure (both on Realm objects & collections). It will give you the same result as JSON.parse(JSON.stringify(objRealm)), but without first serializing & then deserializing.

@christian-hess-94 @YaoHuiJi Sounds like a bug in the RPC calls between the debugger and the device/simulator. We’ll investigate early next week.

@kneth Thanks, I tested 10.4.2 today, and unfortunately, it did not solve our problem. I’ve tested this with React Native versions 0.64.1 and 0.64.2. I have not gone any lower than that since those are the latest versions. Is there a specific version of react native that we should be using?

OMG, same here! I have spent more than 3 hours to locate the weird bug ,but have no clue, the only thing I am quite sure is that it is all about trying to retrieve object(objectForPrimaryKey in my case) under or not under the react native “Debug JS Remotely” mode.

My app use realm 2.29.2 & react native 0.60.5:

After an object with several fields is saved to realm, I use objectForPrimaryKey to retrieve it back,but weird things will happen when objectForPrimaryKey is called,if I turn react native “Debug JS Remotely” on,objectForPrimaryKey will return the right object,but if I turn“Debug JS Remotely” off,objectForPrimaryKey will return a blank object(not undefined). I happened every time with the same result.

A new thing I just discovered. The object’s method sometimes returns the data as an array and sometimes returns all the data as part of an object. The error occurred because I needed to add a snippet of code that transforms the object that is returned into an array so I could iterate over it and render the items in the list.

Funny enough the problem happened because the data was returned as an array, but the snippet of code deleted it. This appears to happen when Realm is executed inside the debugger.

Example: When running the debugger and querying for some data it returns like this (object):

{ 
"0":{
"data": "data1"
},
"1":{
"data": "data2"
}
}

So I had to add a snippet of code to turn it into an array like so:

[
"0":{
"data": "data1"
},
"1":{
"data": "data2"
}
]

When not running the debugger, the data is returned as an array normally:

[
"0":{
"data": "data1"
},
"1":{
"data": "data2"
}
]

So the snippet of code actually transforms it into this: [{}]

Which my component then rendered as an empty item on the list.

Not sure if this is supposed to happen. This also doesn’t seem to be affected by the number of objects being retrieved from the database