Realm-js: realm.object() returning array with empty object

Created on 4 May 2021  路  9Comments  路  Source: realm/realm-js

Goals

Read data after write

Expected Results

That the data be returned after using the function realm.objects ('TermoOrientacao')

Actual Results

A list with empty objects is being returned [{}, {}]

Steps to Reproduce

Write an object in the database and after trying to read

Code Sample

Schema

import { ObjectSchema } from 'realm';

export default class TermoOrientacao {
  public static schema: ObjectSchema = {
    name: 'TermoOrientacao',
    primaryKey: 'id',
    properties: {
      id: 'string',
      agenteFiscalizacaoId: 'string',
      estabelecimentoComercialId: 'string',
      observacao: 'string',
      dataCadastro: 'date',
      dataAlteracao: 'date',
      dataHoraFiscalizacao: 'date',
      codigoFiscalizacao: 'int',
      versao: 'string',
    },
  };

  public id!: string;

  public agenteFiscalizacaoId!: string;

  public estabelecimentoComercialId!: string;

  public observacao!: string;

  public dataCadastro!: Date;

  public dataAlteracao!: Date;

  public dataHoraFiscalizacao!: Date;

  public codigoFiscalizacao!: number;

  public versao!: string;
}

getRealm

import Realm from 'realm';

import TermoOrientacao from './schemas/TermoOrientacao';

export default function getRealm(): ProgressPromise {
  return Realm.open({
    schemaVersion: 1,
    schema: [TermoOrientacao.schema],
  });
}

Write Data

  const saveTermoOrientacao = useCallback(
    async ({
      observacao,
      dataHoraFiscalizacao,
    }: Pick<TermoOrientacao, 'observacao' | 'dataHoraFiscalizacao'>) => {
      try {
        const realm = await getRealm();

        realm.write(() => {
          realm.create<TermoOrientacao>('TermoOrientacao', {
            id: uuid(),
            dataCadastro: new Date(),
            dataAlteracao: new Date(),
            agenteFiscalizacaoId: '123',
            estabelecimentoComercialId: '312',
            codigoFiscalizacao: 123,
            observacao,
            dataHoraFiscalizacao,
            versao: Constants.nativeAppVersion as string,
          });
        });

        realm.close();
      } catch (e) {
        // e
      }
    },
    [],
  );

Read data

  const listTermoOrientacao = useCallback(async () => {
    try {
      setLoading(true);
      const realm = await getRealm();

      const termoOrientacaoRealmObj = realm.objects<TermoOrientacao>(
        'TermoOrientacao',
      );

      termoOrientacaoRealmObj.forEach((item, index) => {
        console.log({
          index,
          item,
          itemId: item.id,
          itemNome: item.observacao,
        });
      });

      console.log('Start List', {
        termoOrientacaoRealmObj: termoOrientacaoRealmObj.values(),
        termoOrientacaoArray: Array.from(termoOrientacaoRealmObj),
      });
      // setTermoOrientacaoData();
    } catch (e) {
      console.log('Error List');
      console.log(e);
    } finally {
      console.log('Finish List');
      setLoading(false);
    }
  }, []);

The logs after write 2 items

{"index": 0, "item": {}, "itemId": undefined, "itemNome": undefined}     
{"index": 1, "item": {}, "itemId": undefined, "itemNome": undefined}     

Start List {"termoOrientacaoArray": [{}, {}], "termoOrientacaoRealmObj":  {}}

Finish List

Version of Realm and Tooling

  • Realm JS SDK Version: 10.3.0
  • Node or React Native: 0.63.4
  • Client OS & Version: Emulator Android 10
  • Which debugger for React Native: ?/None
O-Community

Most helpful comment

@DuCaoTien & @Trosdan react-native-reanimated v2 requires Hermes: https://docs.swmansion.com/react-native-reanimated/docs/installation/#android
Realm does not yet support Hermes.

All 9 comments

Same the issue on android. Look good on ios. Please let me know if you have any solutions.

  • Realm JS SDK Version: 10.4.0
  • Node or React Native: 0.64.0
  • Client OS & Version: Emulator Android 10
  • Which debugger for React Native: ?/None

No solution yet for Android

Sorry, I was too quick. Just for be sure: you only observe it on Android, and with a debugger attached?

Check the logs using metro bundler and Flipper, both return the same result. I tried to show data on the screen and it was returned undefined

Hi @Trosdan, @kneth . My problem was resolved by decreasing the version of react-native-reanimated (2.1.0 -> 1.13.3). I think there are conflicts between the 2 libraries on android. @Trosdan Hope this can helps you if you get the same problem.

@DuCaoTien Changing the version of react-native-reanimated to 1.13.3 solved the problem, I believe that using the bare flow of the expo I needed to make some changes to the native code of android.

```java

// Others imports
import expo.modules.updates.UpdatesController;

import com.facebook.react.bridge.JSIModulePackage;
// import com.swmansion.reanimated.ReanimatedJSIModulePackage; <- this

import java.lang.reflect.InvocationTargetException;
import java.util.Arrays;
import java.util.List;
import javax.annotation.Nullable;

public class MainApplication extends Application implements ReactApplication {
// @Override
// protected JSIModulePackage getJSIModulePackage() { <- this
// return new ReanimatedJSIModulePackage();
// }

// Others override functions

}

@DuCaoTien & @Trosdan react-native-reanimated v2 requires Hermes: https://docs.swmansion.com/react-native-reanimated/docs/installation/#android
Realm does not yet support Hermes.

This seems to be a valid issue.
React-native-reanimated v2.1.0 supports JSC on Android (https://github.com/software-mansion/react-native-reanimated/releases/tag/2.1.0).
Realm.object() returns a list with empty objects [{}, {}] while working together with react-native-reanimated v.2.1.0 on Android.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

max-zu picture max-zu  路  3Comments

emrehayirci picture emrehayirci  路  3Comments

texas697 picture texas697  路  3Comments

MihaelIsaev picture MihaelIsaev  路  3Comments

fever324 picture fever324  路  4Comments