Jonathan
Jonathan

Reputation: 375

TypeScript/Angular: Cannot find name 'XXX'

enter image description here

I have two functions in the same TypeScript component. When I try to call one already declared, VSCode reports that it "[ts] Cannot find name 'XXX'.".


As requested by Tiep Phan, this is the full code:

liveSearchFMs(input: any) {
  this._ectmService.getFMsFromUserSearch(input).subscribe(
    fanmissions => this.fanmissions = fanmissions,
    error => this.errorMessage = <any>error
  );
}

timeout(input) {
  var enteredValue = input;
  var timeout = null;
  clearTimeout(timeout);
  timeout = setTimeout(function () {
    this.liveSearchFMs(enteredValue);
  }, 1000);
}

Upvotes: 0

Views: 7118

Answers (2)

Tiep Phan
Tiep Phan

Reputation: 12596

I guess you wanna create something like this

export class EctmListComponent implements OnInit {
    // other code
    private timeoutTracker;

    timeout(input) {
        if (this.timeoutTracker) {
            clearTimeout(this.timeoutTracker);
        }
        //use arrow function instead
        this.timeoutTracker = setTimeout(() => {
            this.liveSearchFMs(input);
        }, 1000);
        // or store context first
        /*
        const ctx = this;
        this.timeoutTracker = setTimeout(function() {
            ctx.liveSearchFMs(input);
        }, 1000);
        */
        // or using bind method
        /*
        this.timeoutTracker = setTimeout((function() {
            this.liveSearchFMs(input);
        }).bind(this), 1000);
        */
    }
}

Upvotes: 1

You need to use this keyword. so this.liveSearchFMs

Upvotes: 0

Related Questions