This particular question stands out due to the fact that despite similar issues being previously addressed, none of the existing solutions have proven effective. The problem at hand is as follows:
There's a Typescript class that initiates an RxJS.ajaxPost() and encounters an issue with the "this" keyword being undefined within the pipe or callback methods. While arrow functions ()=>{} are typically used to resolve such situations, they do not seem to work in this specific case.
The query then arises: Why is this happening? How can Ajax calls be handled properly while still effectively utilizing responses?
export class Store {
count: number = 0;
getRest(): void {
ajaxPost('https://httpbin.org/delay/2')
.pipe(
tap((response) => {
console.log('response: ', response, this);
this.count += 1;
}),
catchError(error => {
console.log('error: ', error);
return of(error);
})
)
.subscribe((resp) => {
console.log("subs resp", resp, this);
});
}
}