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

311
Visualizações
Property 'error' does not exist on type '"" | Promise<any>'

I'm trying to add some error handling to a service, following the Angular guide.

Relevant snippet:

private handleError (error: Response | any) {
  // In a real world app, you might use a remote logging infrastructure
  let errMsg: string;
  if (error instanceof Response) {
    const body = error.json() || '';
    const err = body.error || JSON.stringify(body);
    errMsg = `${error.status} - ${error.statusText || ''} ${err}`;
  } else {
    errMsg = error.message ? error.message : error.toString();
  }
  console.error(errMsg);
  return Observable.throw(errMsg);
}

However, I'm getting a TypeScript error:

error TS2339: Property 'error' does not exist on type '"" | Promise<any>'. Property 'error' does not exist on type '""'.

I can understand why it's happening-- error.json() returns a Promise<any> and then the subsequent line with body.error wouldn't work because there is no error property. However, it seems like it should expect a JSON object to be returned from .json(). Why is that, and what am I missing that the Angular guide isn't?

about 4 years ago · Santiago Trujillo
3 Respostas
Responde à pergunta

0

Same thing just happened to me. This happens when you fail to import the Response object.

import { Response } from '@angular/http';
about 4 years ago · Santiago Trujillo Relatório

0

No, it won't work because you wrote:

const body = error.json() || '';

Which means that body can be an empty string, and a string doesn't have the error property.

This should be better:

const body = error.json() || { error: null };

Edit

Oh, error.json() returns a Promise, which means that you won't be able to use this synchronous block, instead you'll need to:

error.json().then(body => {
    if (!body) {
        body = "";
    }

    const err = body.error || JSON.stringify(body);
    const errMsg = `${error.status} - ${error.statusText || ''} ${err}`;
});
about 4 years ago · Santiago Trujillo Relatório

0

I had the same issue, I finally found this solution.

.catch(error => {
  let errMsg: string;
  const body = JSON.parse(error._body);
  if (body) {
    errMsg = body.error
  } else {
    errMsg = error.message ? error.message : error.toString();
  }
  console.error(errMsg);
  return Promise.reject(errMsg);
});

about 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