-1

TypeScript を使用して Angular アプリケーションに取り組んでいますが、次の問題があります。

TypeScript クラスには、次のメソッドがあります。

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;
          })
          
          
      )
  }

前のコードを見るとわかるように、スナップオブジェクトでforEach()メソッドを呼び出しています。

このメソッドは、コールバック関数をパラメーターとして受け取ります。このコールバック関数で、次のように、インスタンスに直接定義された別のメソッド ( myCassMethod() ) を実行しようとしています。

this.myClassMethod();

コンソールに次のエラーメッセージが表示されるため、この方法では機能しません。

ERROR TypeError: this is undefined
    findArtistBidsAppliedByCurrentWall notifications.service.ts:187
    node_modules vendor.js:159712
    node_modules vendor.js:145978
    node_modules vendor.js:145609
    node_modules vendor.js:145609
    node_modules vendor.js:145527
    node_modules vendor.js:145977
    node_modules vendor.js:159711
    findArtistBidsAppliedByCurrentWall notifications.service.ts:184
    RxJS 4
    Angular 6
    RxJS 3
    Angular 16
    RxJS 4
    schedule Angular
    RxJS 4
    Angular 20
    node_modules vendor.js:157356
    node_modules NextJS
    node_modules vendor.js:149711
    node_modules vendor.js:149679
    Ur index.cjs.js:5711
    Us index.cjs.js:11891
    step tslib.es6.js:100
    verb tslib.es6.js:81
    fulfilled tslib.es6.js:71
    Angular 13

このコールバック関数内のフォームは、これをインスタンス参照として見ることができないようです。

この問題を解決してmyCassMethod()メソッドを正しく呼び出すにはどうすればよいですか?

4

1 に答える 1