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

142
Visualizações
Refactor from fetch to await that can yield same result

So I moved over a non-reusable fetch request code snippet to my API:

let response = await fetch(visitURL, {
 method: 'POST',
 headers: {
  'Content-Type': 'application/json',
  'Authorization': 'Bearer ' + userJWT
 },
 body: JSON.stringify(endingVisit)
});
if (response.ok) {
  let {visitId, createdAt} = await response.json();
  const viewVisitDto = new ViewVisitDto(`${visitId}${createdAt}${visitorId}${doctorId}${oldPatientId}`);
return viewVisitDto;
} else {
  throw new Error("deactivated!")
}

I was able to get this far:

axios.post(visitURL, {
  headers,
  body: JSON.stringify(visit)
}).then((response) => {
  console.log(response);
}).catch((error) => {
  console.log(error);
})

But does not exactly give me the visitId and createdAt from the response and I cannot use a response.ok nor a response.json(). Essentially I need to pull out that visitId and createdAt that should be coming back in the response.

I also tried just using node-fetch library, but although in VS code it seems to accept it, TypeScript is not happy with it even when I do install @types/node-fetch and even when I create a type definition file for it, my API just doesn't like it.

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

Guessing what you are after is

// don't know axios, but if it returns a promise await it
const dto = await axios.post(visitURL, {
      headers,
      body: JSON.stringify(visit)
    }).then((response) => {
      // parse response
      return {resonse.visitId, resonse.createdAt}
    }).then(({visitId, createdAt}) => {
      // form dto (where are other vals)?
      return new ViewVisitDto(`${visitId}${createdAt}${visitorId}${doctorId}${oldPatientId}`);
    }).catch((error) => {
      console.log(error);
    })

However - you don't mention where doctorId and oldPatientId come from... You try providing more info, including output of the console.log's and the surrounding code

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