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

179
Vistas
Data do not pass to the calling function React-Native

Hello I'm trying to call data from another function. The caller function is this,

 useEffect(() => {
    setFetching(true);
    const fetchMarketData = async () => {
      const marketData = await getMarketData(page);
      if (page === 1) {
        setData(marketData);
        return setFilteredData(marketData);
      } else {
        setFilteredData(data.concat(marketData));
        return setData(data.concat(marketData));
      }
    };
    fetchMarketData();
    if (data != undefined) {
      if (data.length > 0) setFetching(true);
    } else {
    }
    setFetching(false);
  }, [page]);

When I log marketData it returns undefined. But in these functions until return it gives the correct result.

const formatMarketData = (data) =>{
    let formattedData = [];
    data.forEach(item=>{
    const formattedItem = {
        ...item
    }
    formattedData.push(formattedItem);
    });
    return formattedData;
    }

    export const getMarketData = async (page) =>{
            const sending = {"page" : page,};
            const response = await fetch("https://us-central1-cnhtest-38661.cloudfunctions.net/getMarketDatas",{
              headers: {"Content-Type":"application/json"},
              method:"POST",
              body:JSON.stringify({data:sending})
              }).then((res)=>{
                res.json().then((r)=>{
                    return formatMarketData(r.result.data)
                })
            })
}

How can I correctly return this value to my caller function's marketData variable?

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

You forgot to return the first inner promise chain. You need to change

.then((res)=> {
    res.json().then((r)=> {
       return formatMarketData(r.result.data)
     })
})

to the following.

.then((res)=> {
    return res.json().then((r)=> {
       return formatMarketData(r.result.data)
     })
})

Here is an updated code snippet.

const formatMarketData = (data) => {
   let formattedData = [];
   data.forEach(item => {
    const formattedItem = {
      ...item
    }
    formattedData.push(formattedItem);
   });
   return formattedData;
}

const getMarketData = async (page) => {
   const sending = {"page" : page};
   const response = await fetch("https://us-central1-cnhtest-38661.cloudfunctions.net/getMarketDatas", {
        headers: {"Content-Type":"application/json"},
        method: "POST",
        body: JSON.stringify({data:sending})}).then((res)=> {
            return res.json().then((r)=>{
                return formatMarketData(r.result.data)
            })
        })
 return response
}

async function printResult() {
  const result = await getMarketData(1)
  console.log(result)
}

printResult()

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