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

228
Visualizações
Ts2532, Object is possibly 'undefined'

I'm trying to patch a task in a project that I'm creating with the mean stack. All the api's work, but when I try to patch an element, with the id param, there's an error which says:

"Object is possibly 'undefined'".

What I want to do is:

  1. Get the element with a precise id
  2. Use that id as query to patch that element

This is the code:

    export class TaskServicesService {
        
      constructor(private myHttp: HttpClient) { }
    
      async updateTask(payload: any) : Promise<any> {
        const task = await this.myHttp.get('http://localhost:3000/').toPromise();
        const elId: any = task.id;
        return await this.myHttp.patch('http://localhost:3000/list/', {title: payload}).toPromise();
      }
    
    }
about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

Try using optional chaining operator - ?:

const task: any = await this.myHttp.get('http://localhost:3000/').toPromise();
const elId: any = task?.id;

If you are sure that task has value other than null or undefined, you can also use non-null assertion operator - !:

const task: any = await this.myHttp.get('http://localhost:3000/').toPromise();
const elId: any = task!.id;
about 4 years ago · Juan Pablo Isaza Relatório

0

The reason for this error is that the right value (rvalue) of the equation is probably undefined. One way to avoid this error is to check if an object is undefined before using it as a right value. I understand that in your application this error is caused by the task.id object. Hopefully we can fix this problem by:

if(task.id !== null && task.id !== undefined)
{
    const elId: task.id;
    return await this.myHttp.patch('http://localhost:3000/list/', {title: payload}).toPromise();
}
else
{
    /* Error Handling */
}
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