Search code examples
angularng-lightning

ng-lightning - data object is undefined on lookup


I'm working with the Lookup component and am getting an error that my data object is undefined and thus cannot .filter(). Code is below:

getAllAccounts() {
    this._quickAddService.getAllAccounts()
        .subscribe(
        accounts => this.getAllAccountsFinished(accounts),
        error => this.errorMessage = <any>error);
}

getAllAccountsFinished(accounts:any) {
    this.accounts = accounts;
    console.log(this.accounts);

    this.hideSpinner();
}

ngOnInit(){
    this.getAllAccounts();
}

lookup(query: string): Account[] {
    if (!query) {
        return null;
    }

    return this.accounts.filter((item) => item.name.toLowerCase().indexOf(query.toLowerCase())>-1);
}

that console.log is showing that the data is bound properly once the service finishes returning. However when lookup is fired on input this.accounts is undefined.


Solution

  • Answered by @bekos on the Gitter. Need to add binding to component constructor:

    constructor(elementRef:ElementRef, private _quickAddService:QuickAddService) { 
        this.visible = true;
    
        this.lookup = this.lookup.bind(this);
    }