axios response destructuring undefined

lucassmsilva
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

Danila

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.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

axios get response is undefined

axios interceptors response undefined

Axios Undefined Response

axios Vuejs undefined response

Data undefined in axios response

Response undefined for 302 status axios

response is undefined using Axios and Express

error.response is undefined if axios request fails

error.response is undefined in catch axios

axios.get() returns undefined response

axios response object in finally callback is undefined

Response returning undefined react js axios

Axios response is undefined, console log in catch is displayed

Axios response data is undefined while the request is succesful

Axios POST response is undefined but network shows a response object

Destructuring in Typescript with undefined values

Vue + Axios Post request response data returns undefined

Get undefined of response.data from axios that stored in vuex

Axios vue.js CORS Error with proxy undefined response

Vue.js - axios is undefined when trying to store and display vue-axios response data

Strange destructuring assignment, giving undefined

Destructuring fallback to prevent undefined error?

Destructuring of an array of objects getting undefined

nodejs destructuring variables returning undefined

react destructuring variable got undefined

ReactJS + Axios "Uncaught (in promise) Error: Network Error" | "Uncaught TypeError: response.data is undefined"

Axios requests function returning a response as a promise however when consoled its showing undefined

JS/ES6: Destructuring of undefined

Double Destructuring when main object may be undefined