2

I'm having an array customerList: Array<String> = []; and a function which pushes the value from server into this array. It's working fine except when i use .filter() on this array it gives me undefined error. This code is:

customerList: Array<String> = [];

ngOnInit() {
this.customerService.getCustomers().subscribe(res => {
    this.customers = res;
    for (const cust of res) {
      this.customerList.push(cust.first_name);
    }
  }
);
}

findChoices(searchText: string) {
return this.customerList.filter(item =>
  item.toLowerCase().includes(searchText.toLowerCase())
  ); 
 );
}

HTML Code:

<mwl-text-input-autocomplete-container>
  <input type="text" class="form-control" id="customer_name" name="customer_name" aria-describedby="emailHelp"
                 placeholder="Customer name" formControlName="customer_name" mwlTextInputAutocomplete
                 [findChoices]="findChoices"
                 [getChoiceLabel]="getChoiceLabel" autofocus>
</mwl-text-input-autocomplete-container>

The customerList.filter() in findChoices() is giving the error.

P.S.- I'm using this package

8
  • Can you add console.log(this.customerList) to the start of findChoices, and add the output to this question please? Commented Aug 4, 2018 at 18:11
  • @user184994 it's undefined there, but on ngonInit() it is displaying the data Commented Aug 4, 2018 at 18:16
  • Where are you calling findChoices from? Can you add that code as well please? Commented Aug 4, 2018 at 18:17
  • @user184994 added Commented Aug 4, 2018 at 18:21
  • Okay, and if you console.log(this) from inside that function, does it print the name of your component? Commented Aug 4, 2018 at 18:24

2 Answers 2

4

Since you are passing function object findChoices as value to [findChoices], the value of this will not be bound to your component, and hence this.customerList will be undefined.

You can make use of closure to ensure customerList is visible to [findChoices] by following below steps:

  1. Change findChoices implementation to return a function that can be used as value of [findChoices] - that is a function that accepts a string parameter.

    findChoicesIn(list) {
      return (searchText) => 
              list.filter(item => item.toLowerCase().includes(searchText.toLowerCase()));
    };
    
  2. Use the function thus generated as value of [findChoices]

    [findChoices]="findChoicesIn(customerList)"
    
Sign up to request clarification or add additional context in comments.

2 Comments

Working!! Thanks
Fine Tanks Wand!
0

Seams your customerList is empty or null, just add a check

if(this.customerList && this.customerList.length > 0){
  return this.customerList.filter(item =>
  item.toLowerCase().includes(searchText.toLowerCase())
  ); 
}

6 Comments

it is still undefined.
you need to pass the search filter
@Sajeetharan Actually OP is passing a reference to the function, not invoking the function
@user184994 yeah right! it cant be undefined once it holds the value
@Sajeetharan how to pass the search filter?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.