Christopher Jenks Christopher Jenks - 1 year ago 46
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();

module.exports = ServicePlugin;

Answer Source

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.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download