2017-04-03 3 views
1

도와주세요. 데이터베이스에서 메시지를 가져 와서 사용자 프로필 페이지에 표시하는 메시지 서비스가 있습니다. 메시지를 작성하거나 삭제할 때 브라우저를 새로 고칠 때만 변경 사항을 볼 수 있습니다.페이지가 새로 고침 될 때까지 각도가 생성 된 개체를 표시하지 않습니다.

ID를 기준으로 정렬하기 위해 필터를 적용하기 전에 페이지가 동적으로 업데이트됩니다. 필터가 내 메시지 배열을 바인딩 해제하는 이유는 무엇입니까?

프로필 구성 요소 :

export class ProfileComponent implements OnInit { 

    user: User; 
    userId: any; 
    messages: any; 

    constructor(private authService: AuthService, private messageService: MessageService) {} 

    ngOnInit() { 
     this.authService.getUser().subscribe(
     (user: User) => { 
     this.user = user; 
     this.userId = user.userId; 
     } 
    ); 
     this.messageService.getMessages() 
     .subscribe(
      (messages: Message[]) => { 
      this.messages = messages.filter(x => x.userId == this.userId); 
      //this.messages = messages; //this works perfect, but shows all users messages 
      } 
    ); 
    } 
} 
+0

나는 정말로 누군가가 나를 도울 수 있기를 바랬다. 내가 추가하고 삭제하는 동일한 메시지에 가입했기 때문에 자신의 의견이 업데이트되지 않는 이유를 알지 못한다. –

답변

1

이 1 API 호출처럼 보인다 (getUser 방법은) 더 많은 시간이 걸립니다, 그것은 getMessages 방법 후 완료지고 있습니다. 어떻게 든 그들을 종속시켜야합니다. getUsertoPromise 메서드를 사용하여 약속을 준수하도록 변환 할 수 있습니다. 이것에

ngOnInit() { 
    this.authService.getUser() 
    .toPromise() 
    .then(
    (user: User) => { 
     this.user = user; 
     this.userId = user.userId; 
    } 
) 
    .then(() => { 
     this.messageService.getMessages() 
     .subscribe(
     (messages: Message[]) => { 
      this.messages = messages.filter(x => x.userId == this.userId); 
     } 
    ); 
    }); 
} 
+0

감사하지만 여전히 페이지를 새로 고칠 때까지 만들기 및 삭제에 대한 변경 사항을 볼 수 없습니다. 필터를 놓을 때까지 메시지 배열을 완벽하게 처리 할 수 ​​있습니다 .. –

0

변경 코드 : 첫 번째는 호출이 완료되면이 호출 할 수 있도록

messages: Array<any> = []; 
this.authService.getUser().subscribe(
     (user: User) => { 
     this.user = user; 
     this.userId = user.userId; 
     this.messageService.getMessages() 
       .subscribe(
        (messages: Message[]) => { 
        this.messages = messages.filter(x => x.userId === this.userId); 
        //this.messages = messages; //this works perfect, but shows all users messages 
        } 
      ); 
     } 
    ); 

당신은 그냥 둥지에 다른 관찰 가능한이 필요합니다.

+0

이 문제가 해결되지 않았습니다. 내 서비스는 표시 할 메시지 배열을 반환하고 메시지를 정렬하기 위해 필터를 배치하면 원래 배열에서 작동하는 프런트 엔드 코드가 엉망으로 보인다. 나는 잃어버린 유일한 유일한 해결책은 browesr에 다시로드를 호출합니다 : ( –

+0

이 코드를 다시 확인해 주시겠습니까? –

+0

코드는 작동하지만 새로 고칠 때까지 페이지가 결과를 표시하지 않습니다. 각도 2에서 페이지 새로 고침을 강제 실행 하시겠습니까? –