I have called a method in setTimeout()
which in turn calls an HTTP service in Angular.
But when the service method is being called I'm getting the following error: Cannot read properties of undefined (reading '<getUsernamesWithPattern>')
The newFunc()
is called on keyup
event.
newFunc() {
clearTimeout(this.typingTimer);
if (this.myInput) //checking if input value has some data and it's not empty
{
this.typingTimer = setTimeout(this.doneTyping, this.doneTypingInterval);
}
}
This method is being called inside of setTimeout
:
doneTyping() {
//do something
console.log("testing method" + this.myInput.value)
let pattern = this.myInput.value;
console.log(pattern);
this.friendsListService.getUsernamesWithPattern(pattern).subscribe(data => {
console.log(data);
});
}
What am I missing here? Since setTimeout
is an asynchronous function it should be able to call service but it's not able to call the service.
Note: I have declared initialized the friendsListService
in the constructor.