Whenever I invoke the findFromList function from a component, it triggers this particular error message:
ERROR TypeError: Cannot read property 'searchInArray' of undefined at push../src/app/shared/services/filter.service.ts.FilterService.searchInObject (filter.service.ts:40)
The issue seems to stem from the fact that 'this' no longer refers to FilterService within the searchInObject function.
This is the Angular code snippet for FilterService:
import {Injectable, EventEmitter} from '@angular/core';
@Injectable()
export class FilterService {
findFromList(list, keyword) {
return list.filter((el) => this.search(el, keyword));
}
search(el, keyword) {
const type = Array.isArray(el) ? 'array' : typeof el;
const searchFunc = this.getFuncByType(type);
return searchFunc(el, keyword);
}
getFuncByType(type) {
const match = {
'string': this.searchInText,
'number': this.searchInText,
'boolean': this.searchInText,
'array': this.searchInArray,
'object': this.searchInObject,
};
if (typeof match[type] !== 'undefined') {
return match[type];
} else {
throw new Error(`Unknown element type "${type}"`);
}
}
searchInText(text, keyword) {
return (text.toString().indexOf(keyword) !== -1);
}
searchInObject(obj, keyword) {
return this.searchInArray(Object.values(obj), keyword);
}
searchInArray(arr, keyword) {
return arr.find((el) => this.search(el, keyword)) !== undefined;
}
}