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

204
Views
Cannot convert undefined or null to object typescript

I wrote a pipe but when I using the pipe gives an error

HTML

<mat-tab *ngFor="let officer of companies?.officers | valueArray" label="{{officer.name}}">

TS

    import {Pipe, PipeTransform} from '@angular/core';

@Pipe({
  name: 'valueArray',
})
export class ValueArrayPipe implements PipeTransform {
  transform(objects: any = []): any {
    return Object?.values(objects);
  }
}
over 4 years ago · Santiago Trujillo
2 answers
Answer question

0

Use the null-coalescing (??) operator to use a default value if is null or undefined.

Setting a default value for a function argument only works when nothing is passed for that argument into the function, so that should be why your default [] value doesn't work.

...
  transform(objects: any = []): any {
    return Object.values(objects ?? {});
...
over 4 years ago · Santiago Trujillo Report

0

Try accounting for the fact that the input parameter for the transform method can also be null or undefined:

@Pipe({
  name: 'valueArray',
})
export class ValueArrayPipe implements PipeTransform {
  transform(objects: any = []): any {
    if (!objects) {
      return null; // or [], or undefined
    }
    return Object.values(objects);
  }
}
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!