Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

290
Visualizações
angular not showing created object until I refresh page

Please help, I have a message service that is fetching my messages from the database and displaying them on a user profile page. When I create or delete a message, I can only see the changes when I refresh the browser.

The page updates dynamically before I apply a filter to sort by Id, why is the filter unbinding my array of messages?

profile component:

    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
          }
      );
    }
}
about 4 years ago · Santiago Trujillo
2 Respostas
Responde à pergunta

0

Seems like your 1st API call(getUser method) is taking more time, it is getting finished after the getMessages method. You should somehow make them dependent. You could convert getUser Observable to promise using toPromise method.

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);
         }
      );
  });
}
about 4 years ago · Santiago Trujillo Relatório

0

Change your code to this :

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
                  }
              );
        }
      );

You just need to nest the other observables , so it will be called once the first finishes it calls.

about 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda