简体   繁体   中英

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;

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM