Missing Realm constructor


#1

Hi

I am getting this error when I try to require ‘realm’ in my App.

"Missing Realm constructor". Did you run "react-native link realm" ...

These are the steps I took to create my app.

create-react-native-app RealmApp

npm install --save realm

react-native link realm

and then I went to Expo app and started the server.

Code:

import React from 'react';
import { StyleSheet, Text, View } from 'react-native';

const Realm = require('realm');

export default class App extends React.Component {
  render() {

     let realm = new Realm({
       schema: [{
         name: 'Dog',
         properties: {
           name: 'string'
         }
       }]
     });

     realm.write(() => {
       realm.create('Dog', {
         name: 'Rex'
       });
     });

    return (
      <View style={styles.container}>
       <Text>
         Count of Dogs in Realm: {realm.objects('Dog').length}
       </Text>
     </View>
    );
  }
}

const styles = StyleSheet.create({
  container: {
    flex: 1,
    backgroundColor: '#fff',
    alignItems: 'center',
    justifyContent: 'center',
  },
});

#2

Have you tried all the troubleshooting steps here?


#4

Yes, but I had no success.

Does Realm works with create-react-native-app 'project ’ or do I need to use react-native init ‘project’?


#5

Any updates here? Have the same error


#6

I am having the same issue. I’m using create-react-native-app ‘project’ so I can not do the first troubleshooting step which is react-native link realm. Can someone please help?