ОШИБКА TypeError: Невозможно прочитать свойство '1' с нулевым значением в Object.eval [как updateDirectives] - PullRequest
0 голосов
/ 11 ноября 2019

Я столкнулся с ошибкой, которая мне не ясна. Я могу видеть данные для редактирования, но каждый раз появляется такая ошибка. Я пытался поймать ошибку, но я не понимаю, откуда она исходит: каждый console.log работает как исключение. Я думаю, что проблема в том, что где-то данные при первой загрузке равны нулю. И это произойдет только в том случае, если я попытаюсь показать второй ngFor, потому что, если я позволю только первому, то все работает нормально, но не показывает, тогда arrays of comments the second *ngFor.

RROR TypeError: Cannot read property '1' of null at Object.eval [as updateDirectives] (PostsListComponent.html:20) at Object.debugUpdateDirectives [as updateDirectives]

Я получаю данные примерно так.

<tr *ngFor="let post of posts$ | async; trackBy:trackByFunction">
    <td class="title">{{post.title}}</td>
    <td class="dateTime">{{post.body}}</td>
    <div *ngFor="let comment of (groupedComments$ | async)[post.id]; trackBy:trackByFunction">
      <div>
      <td *ngIf="!isEditable(comment)" class="comment">{{comment.name}}</td>
      <textarea class="comment" *ngIf="isEditable(comment)" [(ngModel)]="editableComment.name"></textarea>
      <td *ngIf="!isEditable(comment)"class="comment">{{comment?.body}}</td>
      <textarea class="comment" *ngIf="isEditable(comment)" [(ngModel)]="editableComment.body"></textarea>

      <td class="comment" *ngIf="comment.email === 'Just@do.it' && comment.body.length < 200">
        {{comment.email}}
        <button  *ngIf="!isEditable(comment)" (click)="deleteComment(comment.id)" class="btn btn-danger">Delete</button>
        <button *ngIf="!isEditable(comment)" (click)="editComment(comment)" class="btn btn-info" style="margin-left: 10px">Edit</button>
        <button *ngIf="isEditable(comment)" (click)="update(comment)" class="btn btn-info" style="margin-left: 10px">Update</button>
        <button *ngIf="isEditable(comment)" (click)="cancel()" class="btn btn-danger" style="margin-left: 10px">Cancel</button>
      </td>
      </div>
    </div>
  </tr>

Файл TS.

posts$: Observable<Post[]>;
  comments$: Observable<Comment[]>;
  groupedComments$: Observable<CommentGroup>;
  editableComment = emptyComment();

  constructor(private postsService: PostService,
              private commentsService: CommentService,
              private confirmationDialogService: ConfirmationDialogService) {
    this.getAllData();
  }

  ngOnInit() {

  }
  getAllData() {
    this.posts$ =  this.postsService.getPostsList();
    this.comments$ = this.commentsService.getCommentsList();
    this.groupedComments$ = this.comments$.pipe(
      map(comments => lodash.groupBy(comments, 'postId')),
    );
  }

И это мои услуги.

export class PostService {
  private baseUrl = 'http://localhost:3000/posts';

  constructor(private http: HttpClient) { }
  getPostsList(): Observable<any> {
    return this.http.get(`${this.baseUrl}`);
  }
}
export class CommentService {
private baseUrl = 'http://localhost:3000/comments';

constructor(private http: HttpClient) { }

getComment(id: number): Observable<any> {
return this.http.get(`${this.baseUrl}/${id}`);
}

getCommentsList(): Observable<any> {
return this.http.get(`${this.baseUrl}`).pipe(catchError(this.errorHandler));
}
}

1 Ответ

1 голос
/ 11 ноября 2019

groupedComments$ | async - это null до получения ответа http, поэтому.

...