Currently, I am troubleshooting a challenge in my Angular application that involve TypeScript. The issue lies within a method in a TypeScript class:
findArtistBidsAppliedByCurrentWall(bid):Observable<Bid[]> {
console.log("findArtistBidsAppliedByCurrentWall() START")
let data:Array<any> = null;
return this.db.collection('bids',
ref=> ref.where("wallId", "==", bid.wallId))
.get()
.pipe(
map(snaps => {
snaps.forEach(function(doc) {
console.log("BLA: ", doc.id, " => ", doc.data());
this.myClassMethod();
return doc.data;
});
return null;
})
)
}
In the above code snippet, there is a call to a forEach() method on the snaps object.
This method requires a callback function as input. Within this callback function, an attempt is made to execute another method (myCassMethod()) defined directly within the instance, like so:
this.myClassMethod();
However, this approach is not effective as it triggers an error message in the console:
ERROR TypeError: this is undefined
findArtistBidsAppliedByCurrentWall notifications.service.ts:187
node_modules vendor.js:159712
...
Evidently, the callback function cannot properly access the this reference as an instance.
To resolve this issue and successfully invoke the myCassMethod(), what steps can be taken?