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

224
Vistas
The result of an async function is undefined in JS using AXIOS

I'm trying to fetch data on my client-side from the server-side which is connected to MongoDB.

I'm using React on the front end and Axios for the HTTP requests.

I have 2 files, one for the API and one is the index.jsx of the app.

I successfully fetched the data from the DB but the result I get on the index.jsx is always undefined.

The API FILE:

export async function  getNotesFromDB(googleId) {
let answer;
await axios
    .get(url + "/note/" + googleId, { withCredentials: true }) //WHEN LOCAL : http://localhost:5000/note/
    .then((notesDB) => {
        answer = notesDB; 
    })
    .catch((error) => {
        //Indicates the client of an error getting the notes from
        console.log(error);
         answer= null;
    })
    .finally( () => {
        return answer;
    });

}

The index.jsx file :

import { getNotesFromDB as getNotesFromAPI } from "../API/Notes.jsx";
async function getNotesFromDB() {
    if (userInfo) {
      let googleId = userInfo.googleId;
      const result = await getNotesFromAPI(googleId);
      console.log(result);
 } else {
      history.push("/");
    }
  };
about 4 years ago · Juan Pablo Isaza
2 Respuestas
Responde la pregunta

0

You are returning nothing from the getNotesFromDB function, you should return the result of the axios call:

export async function  getNotesFromDB(googleId) {
  let answer;
  return await axios
  // Rest of the function body ....
about 4 years ago · Juan Pablo Isaza Denunciar

0

you can just return the promise and handle the error

export function getNotesFromDB(googleId) {
return axios
    .get(url + "/note/" + googleId, { withCredentials: true }) //WHEN LOCAL : http://localhost:5000/note/
    .catch((error) => {
        //Indicates the client of an error getting the notes from
        console.log(error);
         return null
    })
}

or

export const getNotesFromDB = (googleId)  => axios
    .get(url + "/note/" + googleId, { withCredentials: true }) //WHEN LOCAL : http://localhost:5000/note/
    .catch((error) => {
        //Indicates the client of an error getting the notes from
        console.log(error);
         return null
    })

or if you prefer to use async/await

export async function  getNotesFromDB(googleId) {
try{
     const res = await axios.get(url + "/note/" + googleId, { withCredentials: true })
     return res 
   }catch(e){
     console.error(e);
     return null;
   }
}
    
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