'Cannot read properties of undefined' method inside another

Viewed 450

I have the following typescript class

export default class BaselineCtrl {
  constructor() {}

  async foo() {
    this.bar();
  }

  async bar() {}
}

When I call the method via

baselineCtrl = new BaselineCtrl();
this.baselineCtrl.foo();

Im getting the error

TypeError: Cannot read properties of undefined (reading 'bar')

What am I doing wrong?

2 Answers

baselineCtrl and this.baselineCtrl refer to two different things, the first one is a local variable (presumably) and the second one is a property on the current object. I'm not sure which one of them you want, it depends on what you're trying to do, but you definitely want the same one in both places.

Related