Uploaded image for project: 'Realm JavaScript SDK'
  1. Realm JavaScript SDK
  2. RJS-33

"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

            Assignee:
            Unassigned Unassigned
            Reporter:
            unitosyncbot Unito Sync Bot
            Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

              Created:
              Updated: