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

143
Visualizações
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);
  }
}
about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

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 ?? {});
...
about 4 years ago · Juan Pablo Isaza Relatório

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);
  }
}
about 4 years ago · Juan Pablo Isaza 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