realm-js: "realm.objectForPrimaryKey" result missing `addListener` function

Bug

For React Native, the result from realm.objectForPrimaryKey should have the addListener function present, but it is missing from the resulting object if the schema is defined using classes.

Expected Results

When I do the below, I expect the addListener function to be present, and log true.

const person = realm.objectForPrimaryKey('Person', id);
console.log(person.addListener != null);

Actual Results

person.addListener is undefined, so it logs false.

Steps to Reproduce

Please note I have used classes with static schema properties to define the models. I have read the docs, and am aware that there is a large warning that says classes are not fully supported, but should work in react-native, so I used them anyways.

Code Sample

// App.js - Created from an ejected expo app
import React from 'react';
import { StyleSheet, Text, View } from 'react-native';
import Realm from 'realm';

const DogSchema = {
  name: 'Dog',
  primaryKey: 'id',
  properties: {
    id: 'int',
    name: 'string',
  },
}
class Cat { }
Cat.schema = {
  name: 'Cat',
  primaryKey: 'id',
  properties: {
    id: 'int',
    name: 'string',
  },
}

const schema = [
  DogSchema, // This works
  Cat        // This doesn't
]

export default class App extends React.Component {

  constructor(props) {
    super(props);
    this.state = {
      realm: null,
      dog: undefined,
      cat: undefined,
    };
  }

  componentWillMount() {
    Realm.open({ schema }).then(realm => {
      realm.write(() => {
        realm.create('Dog', { name: 'Rex', id: 0 }, true);
        realm.create('Cat', { name: 'Tilly', id: 0 }, true);
      });
      this.setState({ 
        realm,
        dog: realm.objectForPrimaryKey('Dog', 0),
        cat: realm.objectForPrimaryKey('Cat', 0),
      });
    });
  }

  render() {
    let dogInfo = 'Loading...';
    let catInfo = '';
    if (this.state.realm) {
      dogInfo = `dog.addListener exist? ${this.state.dog.addListener != null}`;
      catInfo = `cat.addListener exist? ${this.state.cat.addListener != null}`;
    }

    return (
      <View style={styles.container}>
        <Text>{dogInfo}</Text>
        <Text>{catInfo}</Text>
      </View>
    );
  }
}

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

This results in:

Version of Realm and Tooling

  • Realm JS SDK Version: 3.0.0-beta.1
  • Node: 10.15.3
  • React Native: 0.57.8
  • Client OS & Version: Android 9
  • Which debugger for React Native: None

About this issue

  • Original URL
  • State: open
  • Created 5 years ago
  • Comments: 15 (4 by maintainers)

Most upvoted comments

@Fawxy I’ve made a npm library to better support TypeScript class based models, including the addition of decorators. This should be easier than me posting all my setup, interfaces, and classes.

Check it out here: realm-ts-class-decorators

@model("Cat")
export default class Cat extends RealmModel {
  @property({ type: "int", primaryKey: true }) public id!: number;
  @property("string") public name!: string;
}

Sorry I should’ve been more specific: extending Object.realm works now, and this in turn prevents the “missing method” issue. Extending Object.realm used to throw TS errors.

@kneth Thanks for the help, I’ll go with just using the schema object for now.