Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

131
Vistas
How can I destructure react query async return variables so that one of them doesn't log undefined?

The Axios object doesn't have a winnersData property, so destructuring it returns undefined in the original block of ocde. I need to destructure data from both Axios requests and what I call them later on needs to be different. I'm just not sure how to implement this and have been struggling for a while. My attempt below fails as well.

How can I implement this?

Original block of code:

async function fetchUploads(){
        const headers = {
            "Accept": 'application/json',
            "Authorization": `Bearer ${authToken}`
        };

        const {data} = await axios.get('http://localhost/api/get-user-uploads-data', {headers});
        return data;
    }

async function fetchWinners(){
        const headers = {
            "Accept": 'application/json',
            "Authorization": `Bearer ${authToken}`
        };

        const {winnersData} =  axios.get('http://localhost/api/choose-winners', {headers});
        return winnersData
    }

    const { data }        = useQuery('uploads', fetchUploads)
    const { winnersData } = useQuery('winners', fetchWinners)
    console.log(data); // logs data correctly
    console.log(winnersData); // logs undefined

    return(....);

Attempted fix for fetchWinners() I've tried but it failed:

async function fetchWinners(){
    const headers = {
        "Accept": 'application/json',
        "Authorization": `Bearer ${authToken}`
    };

    const { winnersData } =  axios.get('http://localhost/api/choose-winners', {headers});
    return { winnersData };
}



const { data }        = useQuery('uploads', fetchUploads)
const { data: { winnersData } } = useQuery('winners', fetchWinners)
console.log(data.winnersData); // still logs undefined
about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

Looks like useQuery always returns a property called data, so you just need to rename it (same for the axios return). And don't forget an await for the axios call.

const { data } = await axios.get('http://localhost/api/choose-winners', {headers});
return data;
...
const { data } = useQuery('uploads', fetchUploads);
const { data: winnersData } = useQuery('winners', fetchWinners);
console.log('winnersData', winnersData);
about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda