"realm.objectForPrimaryKey" result missing `addListener` function
See original GitHub issueBug
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
Issue Analytics
- State:
- Created 4 years ago
- Comments:15 (4 by maintainers)
Top Results From Across the Web
Realm.Results - MongoDB
Function to execute on each object in the collection. If this function returns true , then the index of that object will be...
Read more >How to use the realm.open function in realm - Snyk
To help you get started, we've selected a few realm.open examples, based on popular ways it is used in public projects.
Read more >Computed property in realm. How to update a different realm ...
addListener ((messages, changes) => { for (const index of changes.insertions) { const channel = realm.objectForPrimaryKey( "channel", messages[ ...
Read more >Class: Collection - Realm
addListener ((collection, changes) => { // collection === wines ... Function to execute on each object in the collection. If this function returns...
Read more >realm-ts-class-decorators - NPM Package Overview - Socket.dev
Functions like addListener or isValid on models from realm.objectForPrimaryKey will NOT be present (See realm-js #2430 for more info).
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
@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
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.