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

241
Views
axios response destructuring undefined
export async function downloadAndSyncData(
  user_id: number
): Promise<boolean> {
  try {
    const { status, data } = await getProtocols(user_id);
    let { sets } = data;
    if (status === 200) {
      if (sets !== undefined) {
        console.log("entrou aqui");
        sets.map(async set => {
          const callDetail = detailData(set);
          let obj = await insertDataStorage("DetailSchema", callDetail);
        });
      } else {
        console.log("undefined sets");
      }
      return true;
    } else {
      return false;
    }
  } catch (error) {
    throw error;
  } finally {
    await syncOptions();
  }
}

I have a function on my app that make a request from sets to show on a list. sets is an Array of Objects, that comes from my backend on laravel. laravel return

The thing is, sometimes that work, sometimes not. And i didn't understand why. It works with small objects.

test console

On postman, its ok too. postman

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Move destructuring inside if (status === 200) { check. If the status is not 200 then the data might be undefined and you cannot destructure undefined value.

Or you can rewrite your check like that: let sets = data?.sets;, that way you will first check that the data is here, then get sets property value.

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!