Search code examples
androidreact-nativerealm

Multiple errors when trying to import Realm into React Native app


I'm trying to build an RN app focusing on Android. I decided to use realm seeing all the good reviews it has. So, I added the import and package in the MainActivity and ran react-native link realm, but I still get some errors.

Here's my code so far:

import Realm from 'realm'
import _ from 'lodash'
const profileId = 'e72d7aa7-43e4-4095-a5c0-71fe08f6c4df';

var realm = new Realm({
    schema: [{
      name: 'Profile',
      properties: {
      id: 'string',
      profileVersion: 'number',
      firstName: 'string',
      lastName: 'string',
      profilePicBase64: 'string'
    }
  }
]});

if(_.head(realm.objects('Profile')) === undefined){
  realm.write(() => {
    realm.create('Profile', {
      id: profileId,
      profileVersion: -1,
      firstName: '',
      lastName: '',
      profilePicBase64: ''
    })
  })
}

export default {
  getProfile : () => _.head(realm.objects('Profile')),
  setProfile: (profile) => {
    realm.write(() => {
      profile.id = profileId;
      realm.create('Profile', profile, true)
    })
  }
}

I intend to use the dbContext as a single point of access to the db and to abstract this layer a bit

Now, if I run the app normally (not step by step in the debugger), I get: Missing Realm constructor - please ensure RealmReact framework is included! in the RSOD (red screen of death). (which has it's root in the index.js file in the realm node module)

If I run it in the debugger, it fails in rpc.js when trying to access localhost:8082 with

Code 19 POST http://localhost:8082/create_session net::ERR_CONNECTION_REFUSED

when executing the lines:

request.open('POST', url, false);
request.send(body);

Besides what's written in the docs, I haven't done anything else for realm to work.

Any ideas on how to fix this?


Solution

  • After discussing with Scott Kyle on github, I found that the issue was caused by a rename I did to the RN project, without modifying the xml configs.

    I'm posting the discussion on github, so that it might help some other noob like me:)