Realm.objects() in react-native app return empty objects

Viewed 1440

I'm creating a db in realm, but when i want to consult the data with Realm.objects() return empty objects, like this: {"0":{},"1":{},"2":{}...etc}, my code is the next:

import Realm from "realm";
const nombreEsquema = 'Imagen12';
class Imagen {}

let esquema = Imagen.schema = {
   name: nombreEsquema,
   properties: {
      nombre: {
         type: 'string'
      },
      uri: {
         type: 'string'
      },
      url: {
         type: 'string'
      },
   }
};

let dbRealm = Realm.open({
   schema: [esquema]
});

functionRealm() {
   dbRealm.then(realm => {
      realm.write(() => {
         realm.create(nombreEsquema, {
            nombre: 'David',
            url: 'My URL',
            uri: 'My URI'
         });
      });

      let images = realm.objects(nombreEsquema);
      console.log("------------------------------");
      for (let i of images) {
         console.log(i);
      }
   });
}

i read the realm's documentation but i don't see anything with that problem, my realm version is 5.0.2, and i don't know what i'm doing bad, i hope you can help me with my problem.

2 Answers

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

<------------------------------------- || **Optional ** || ------------------------------------->

**You shouldn't have to do this, but just incase...

  1. 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!

Related