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

147
Views
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 answers
Answer question

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 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!