读取“ id”时出错

时间:2018-07-30 04:50:41

标签: angular

我有时会出错:

  

错误TypeError:无法读取未定义的属性“ id”       在评估时(notebook.component.ts:88)       在Array.filter()       在SafeSubscriber.eval [作为_next](notebook.component.ts:88)       在SafeSubscriber .__ tryOrUnsub(Subscriber.js:243)       在SafeSubscriber.next(Subscriber.js:190)       在Subscriber._next(Subscriber.js:131)       在Subscriber.next(Subscriber.js:95)       在MapSubscriber._next(map.js:85)       在MapSubscriber.Subscriber.next(Subscriber.js:95)       在RefCountSubscriber.Subscriber._next(Subscriber.js:131)

页面加载后发生错误。如果页面被重新加载,它将消失。怎么能完全摆脱它?

ts:

  private loadNotebooks() {
    let filteredNotebooks;
    if (this.servNotebook) {
        this.servNotebook.getNotebooks().subscribe(notebook => {
            this.notebooks = notebook;
            this.filteredNotebooks = this.notebooks.filter((notebook) => notebook.user_id == this.authTokenService.currentUserData.id);
        });
    }
  }

html:

<ng-container *ngFor="let notebook of filteredNotebooks">
  <ng-container *ngIf="authTokenService.currentUserData">
    <div class="col-md-6">
      <div class="card">
        <div class="header">
          <h4 class="title">{{notebook.title}}</h4>   
        </div>
        <div class="content" >
          <div [innerHTML]="notebook.description">
            {{ notebook.description }}
          </div>
          <hr />
          <div class="footer">
            <div class="stats">
              {{notebook.created_at | date: 'medium'}} 
            </div>
          </div>   
        </div>
      </div>
    </div>
  </ng-container>
</ng-container>

身份验证服务:

  import { Injectable } from '@angular/core';
  import {Angular2TokenService} from 'angular2-token';
  import {Response} from '@angular/http';
  import {Subject} from 'rxjs/Subject';
  import {Observable} from 'rxjs/Observable';
  import 'rxjs/add/operator/map';

  @Injectable()
  export class AuthService {

    userSignedIn$:Subject<boolean> = new Subject();

    constructor(private authService:Angular2TokenService) {
      this.authService.validateToken().subscribe(
        res => res.status === 200 ? this.userSignedIn$.next(res.json().success) : this.userSignedIn$.next(false)
    );
  }

    logOutUser():Observable<Response>{

      return this.authService.signOut().map(
          res => {
            this.userSignedIn$.next(false);
            return res;
          }
      );
    }

    logInUser(signInData: {email:string, password:string}):Observable<Response>{

      return this.authService.signIn(signInData).map(
          res => {
            this.userSignedIn$.next(true);
            return res
          }
      );

    }

    registerUser(signUpData:  {email:string, password:string, passwordConfirmation:string}):Observable<Response>{
      return this.authService.registerAccount(signUpData).map(
          res => {
            this.userSignedIn$.next(true);
            return res
          }
      );
    }

  }

0 个答案:

没有答案