Christopher Jenks Christopher Jenks - 4 months ago 10
Javascript Question

Why do I get property of undefined error?

I am getting the following error.

Uncaught TypeError: Cannot read property 'on' of undefined


it happens when I add in the line of code

let ServicePlugin = new ServicePlugin();


Any Idea what I am doing wrong? Below is the full code.

'use strict';

class ServicePlugin {
constructor(api) {
this._api = api;

this._api.on('ready', () => this._apiReady());
this._api.on('saveSuccess', () => this._apiSuccess());
}

_apiReady() {
console.log('Plugin Ready')
}

_apiSuccess() {
console.log('Plugin Success');
}

}

let ServicePlugin = new ServicePlugin();
ServicePlugin._apiReady();

module.exports = ServicePlugin;

Answer

You must provide the learnosity parameter (whatever that is) when you instantiate your class. AnnServicePlugin expects it:

let annServicePlugin = new AnnServicePlugin(learnosity);

Of course, the learnosity object must have the expected on method.