question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Cannot call addListener when using a ES2015 class as schema

See original GitHub issue

Goals

Use a class as schema (as shown per https://realm.io/docs/javascript/latest/#classes) and be able to use objects the same manner as if the schema was defined as an object.

Expected Results

Be able to call .addListener on an object that is a result of realm.create or realm.objects when the schema was defined using a ES2015 class.

Actual Results

ExceptionsManager.js:86 person.addListener is not a function
handleException @ ExceptionsManager.js:86
handleError @ setUpErrorHandling.js:23
reportFatalError @ error-guard.js:42
guardedLoadModule @ require.js:197
metroRequire @ require.js:122
(anonymous) @ RealmTest2.js:33
executeApplicationScript @ debuggerWorker.js:40
(anonymous) @ debuggerWorker.js:72
RCTLog.js:47 Running application Tripps ({
    initialProps =     {
    };
    rootTag = 11;
})

Code Sample

import Realm from "realm"

class Person {
  get fullName() {
    return this.firstName + " " + this.lastName
  }
}

Person.schema = {
  name: "Person",
  properties: {
    firstName: "string",
    lastName: "string",
  },
}

const realm = new Realm({
  path: "realm-test.realm",
  schema: [Person],
})

realm.write(() => {
  realm.create(Person.schema.name, {
    firstName: "John",
    lastName: "Smith",
  })
})

const persons = realm.objects(Person.schema.name)
const person = persons[persons.length - 1]
console.log(person.fullName)

person.addListener(obj => { // Error: person.addListener is not a function
  if (obj) {
    console.log("Changes Found! " + obj.fullName)
  }
})

Version of Realm and Tooling

  • Realm JS SDK Version: 2.29.2
  • Node / React Native: 10.16.3 / 0.60.4
  • Client OS & Version: iPhone SE / iOS 12.1 simulator
  • Which debugger for React Native: Chrome

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:5 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
djMaxcommented, Jun 24, 2020

Could be better??? It doesn’t work at all.

0reactions
knethcommented, Aug 27, 2019

@mattijsf Yeah, the Typescript support could be better. Please create an issue about your Typescript observations.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Javascript - ES6 : addEventListener call a class methode with ...
The problem is that it's not working correctly with the paramter "button_id" in the class call "this.bu_clicked()". By starting the Script the ...
Read more >
15. Classes - Exploring JS
1. Why can't classes be function-called? 15.9.2. How do I instantiate a class, given an Array of arguments?
Read more >
Realm: Create reactive mobile apps in a fraction of the time
It works in React Native but not in Node. If you want to use ES2015 classes (and maybe want to inherit existing features),...
Read more >
ES5 to ESNext — here's every feature added to JavaScript ...
Before ES2015, ECMAScript specifications were commonly called by their ... A variable initialized with var outside of any function is ...
Read more >
JavaScript Class constructor Method - W3Schools
Note: A class cannot have more than one constructor() method. This will throw a SyntaxError . You can use the super() method to...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found