Home > Back-end >  ERROR TypeError: Cannot read properties of undefined (reading 'removeVietnameseTones') in
ERROR TypeError: Cannot read properties of undefined (reading 'removeVietnameseTones') in

Time:03-28

I'm new to Angular.

I have a function which is running normally in ReactJS with JS.

const _onSearch = value => {
  onTextChange(value);

  let filtered = drivers.filter(function (car) {
    return removeVietnameseTones(car.DriverName)
      .toLowerCase()
      .includes(removeVietnameseTones(value).toLowerCase());
  });

  setFiltered(filtered);
};

But I have try it in Angular with TS

onSearch(value: string): void {
  this.searchValue = value;
  console.log(this.removeVietnameseTones(value)); // this line run

  this.outputData = this.outputData.filter(function (item) {
    console.log(this.removeVietnameseTones(value)); // this line error
    console.log(this.removeVietnameseTones(item.unitName)); // this line error

    return this.removeVietnameseTones(item.unitName) // this line error
      .toLowerCase()
      .includes(this.removeVietnameseTones(value).toLowerCase());
  });
}

it's undefined.

ERROR TypeError: Cannot read properties of undefined (reading 'removeVietnameseTones')

So where did I go wrong?

My demo.

CodePudding user response:

use Arrow Function instead of Function

filter(function (item) {}) => filter((item) => {})

  • Related