Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/447.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 为什么我不能从这个回调函数的内部调用类方法?错误类型错误:这是未定义的_Javascript_Typescript - Fatal编程技术网

Javascript 为什么我不能从这个回调函数的内部调用类方法?错误类型错误:这是未定义的

Javascript 为什么我不能从这个回调函数的内部调用类方法?错误类型错误:这是未定义的,javascript,typescript,Javascript,Typescript,我正在使用TypeScript开发一个Angular应用程序,我有以下问题 在TypeScript类中,我有以下方法: findArtistBidsAppliedByCurrentWall(bid):Observable<Bid[]> { console.log("findArtistBidsAppliedByCurrentWall() START") let data:Array<any> = null; return this.db.

我正在使用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;
          })
          
          
      )
  }
这样做不起作用,因为我正在控制台中获取以下错误消息:

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
在这个回调函数中,似乎无法将this视为实例引用

如何解决此问题并正确调用myCassMethod方法§?

您可以尝试将snaps.forEachfunctiondoc{更改为snaps.forEach doc=>{但我不能完全确定它是否能工作

或者通过设置

findArtistBidsAppliedByCurrentWall(bid):Observable<Bid[]> {
   const that = this;

使用它。这一个可以保证工作。

JavaScript中的这个并不是指实例的类,它通常指的是实例本身。请看@Teemu是的,我指的是实例本身……但在这个回调函数中,我不能使用它:如果为forEach提供了thisArg参数,它将用作回调的this值。t回调最终可观察到的thisArg值是根据确定的。尝试使用该选项,但现在它似乎没有进入方法…它没有给我错误,但它没有执行打印日志的方法代码
findArtistBidsAppliedByCurrentWall(bid):Observable<Bid[]> {
   const that = this;