Angular“无法读取未定义的属性'订阅'”

新手上路,请多包涵

在我开始我的问题之前,我想让你知道我已经做了一个很大的研究,但我找不到解决方案(解释)为什么会出现这个错误。

另请注意,我是 Angular 的新手,我刚刚开始学习它是如何工作的。

所以,我遇到的问题是我在这个问题的标题中输入的内容。

我尝试做的是基于我在 Udemy 上购买的课程,使用 Firebase 构建登录系统。

我使用的代码如下:

auth.service.ts

 import {Injectable} from '@angular/core';
import * as firebase from 'firebase';

@Injectable ()
export class AuthService {
    token: string;

    // ...

    singInUser ( email: string, password: string ) {
        // login process here ...
    }

    // Responsible to retrieve the authenticated user token
    getToken () {
        return firebase
            .auth ()
            .currentUser
            .getIdToken ();
    }
}

数据存储.service.ts

 // ... Dependencies here
@Injectable ()
export class DataStorageService {
    private recipeEndPoint: string = 'https://my-unique-id.firebaseio.com/recipes.json';
    private recipeSubscription: Observable<any> = new Observable();

    constructor ( private http: Http,
                  private recipes: RecipeService,
                  private authService: AuthService ) {}

    // other functionality ...

    getRecipes () {
        const token = this.authService.getToken ();

        token.then (
            ( token: string ) => {
                this.recipeSubscription = this.http.get ( this.recipeEndPoint + '?auth=' + token ).map (
                    ( data: Response ) => {
                        return data.json ();
                    }
                );

                // THIS PARTICULAR CODE WORKS AS EXPECTED
                // WITH NO ISSUES
                this.recipeSubscription.subscribe (
                    ( data: Response ) => {
                        console.log ( 'Data response: ', data );
                    },
                    ( error ) => {
                        console.log ( 'Error: ' + error );
                    }
                )
            }
        );

        // This is supposed to return an Observable to the caller
        return this.recipeSubscription;
    }
}

header.component.ts

 // Dependencies here ...

@Component({
  selector: 'app-header',
  templateUrl: './header.component.html',
  styleUrls: ['./header.component.css']
})
export class HeaderComponent implements OnInit {
  constructor(private dataStorage: DataStorageService, private recipeService: RecipeService) { }

  // Other Code Here ...

  onFetchData() {
    let recipeSubscription = this.dataStorage.getRecipes();

    // THIS RETURNS TRUE
    console.log(recipeSubscription instanceof Observable);

    // THIS LINE THEN RETURNS THE MESSAGE:
    // ERROR TypeError: Cannot read property 'subscribe' of undefined
    recipeSubscription.subscribe();

    // IF I COMMENT OUT THE PREVIOUS LINE
    setTimeout(
      () => {
        // THIS RETURNS TRUE
        console.log(recipeSubscription instanceof Observable);
      },
      500
    );

    setTimeout(
      () => {
        // AS WELL THIS ONE RETURNS TRUE
        console.log(recipeSubscription instanceof Observable);
      },
      1000
    );

    setTimeout(
      () => {
        // AS WELL THIS ONE RETURNS TRUE
        console.log(recipeSubscription instanceof Observable);
      },
      1500
    );
  }
}

所以,不幸的是,我看不出这段代码有什么问题。谁能发现我做错了什么?

注意: 我删除了部分代码只是为了使片段更具可读性。如果您需要任何其他部分,请随时问我,我会在这里提供。

更新#1

这就是它的样子 header.component.html

 <nav class="navbar navbar-default">
    <div class="container-fluid">
        <div class="navbar-header">Logo Here</div>

        <div class="navbar-default">
            <ul class="nav navbar-nav">
                <!-- Left Navigation Options -->
            </ul>
            <ul class="nav navbar-nav navbar-right">
                <!-- Right Navigation Options -->
                <li class="dropdown" appDropdown>
                    <a routerLink="/" class="dropdown-toggle" role="button">Manage <span class="caret"></span></a>
                    <ul class="dropdown-menu">
                        <li>
                            <a style="cursor: pointer;" (click)="onSaveData()">Save Data</a>
                        </li>
                        <li>
                            <!-- Here is where I call the onFetchData method -->
                            <a style="cursor: pointer;" (click)="onFetchData()">Fetch Data</a>
                        </li>
                    </ul>
                </li>
            </ul>
        </div>
    </div>
</nav>

原文由 KodeFor.Me 发布,翻译遵循 CC BY-SA 4.0 许可协议

阅读 269
2 个回答

问题似乎是您的代码执行的顺序,更具体地说是 getRecipes() 方法:

 // Numbers indicate the execution order

getRecipes () {
    const token = this.authService.getToken ();

    // 1. You call a promise, which will take a while to execute...
    token.then (
        ( token: string ) => {
            // 3. Finally, this bit gets executed, but only when the promise resolves.
            this.recipeSubscription = ...
        }
    );

    // 2. Then, you return a variable that hasn't been assigned yet,
    // due to the async nature of the promise.
    return this.recipeSubscription;
}

解决方案是您的 getRecipes () 方法不应该订阅。它应该 返回一个 Promise 或一个 Observable

像这样的东西:

 getRecipes() {
    // Convert the initial promise into an observable
    // so can you use operators like map(), mergeMap()... to transform it.
    const tokenObs = Observable.fromPromise(this.authService.getToken());

    // Merge the token observable into an HTTP observable
    // and return the JSON data from the response.
    return tokenObs
      .mergeMap(token => this.http.get('XXX?auth=' + token))
      .map(resp => resp.json());
}

然后, HeaderComponent 中的调用代码变成:

 const recipeObs = this.dataStorage.getRecipes();
recipesObs.subcribe(jsonData => {
  // Use the JSON data from the HTTP response
});

几点说明:

  • 您需要显式导入代码中使用的 RxJS 运算符。如果您按照我的示例进行操作,则需要在开头添加以下导入:
 import 'rxjs/add/observable/fromPromise';
import 'rxjs/add/operator/map';
import 'rxjs/add/operator/mergeMap';

  • 你永远不应该订阅创建 observable 的方法。在你的情况下,不要订阅 getRecipes() 。总是在最后一刻订阅。您可以多次订阅同一个 observable,但请注意,每个订阅都会重新执行 observable(在 http 请求的情况下,这意味着您多次运行请求;不理想……)。
  • 调用变量 recipeSubscription 不是一个好主意,因为它包含 Observable ,而不是 Subscription 。订阅是 subscribe() 返回的内容。换句话说: const subscription = observable.subscribe()
  • 我看到您直接使用 Firebase SDK。你知道 AngularFire 库 吗?

原文由 AngularChef 发布,翻译遵循 CC BY-SA 3.0 许可协议

我能够通过将可观察值绑定到主题来解决这个问题。

 observable = behaviourSubject.asObservable();

我在构造函数中添加了这个。

下面是代码的骨架。

   subjectVar: BehaviorSubject<type> = new BehaviorSubject<type>({} as type)
  observableVar: Observable<type>

  constructor(){
     this.observableVar = this.subjectVar.asObservable();
  }

  pulic function()Observable<type>{
      foo();
      return observableVar

  }

  foo(){
     let var = someValue
     subjectVar.next(var)
  }

原文由 AMAL MOHAN N 发布,翻译遵循 CC BY-SA 4.0 许可协议

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进