Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

169
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!