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

170
Visualizações
In which part of the code should I check for undefined

There is a method

product$!: Observable<Product>;

  getProduct(): void {
    this.product$ = this.route.params
      .pipe( switchMap( params => {
        return this.productServ.getById(params['id'])
      }))
  }

which returns, an object of type

export interface Product {
  type?: string
  id?: string
  title?: string
  date?: Date | undefined;
}

and a template that displays the properties of this object

<div *ngIf="product$ | async as product; else loading">
  <h2>{{ product.title}}</h2>

As a result of calling the (getProduct) method, the variable this.product$ may be undefined. Therefore, IDEA throws an error in this line of the template

<h2>{{ product.title}}</h2>

How is this problem solved correctly

  • in the template, add a check *gif="product.title"
  • or this problem should be solved in the code, then the question is how to do it correctly
over 4 years ago · Santiago Trujillo
1 Respostas
Responde à pergunta

0

Did you try *ngIf="(product$ | async) as product; else loading"?

Also you can define an empty observable as product$ = Observable.of<Product>(Product()) before rendering

over 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