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

217
Vistas
El resultado de una función asíncrona no está definido en JS usando AXIOS

Estoy tratando de obtener datos en mi lado del cliente desde el lado del servidor que está conectado a MongoDB.

Estoy usando React en el front-end y Axios para las solicitudes HTTP.

Tengo 2 archivos, uno para la API y otro es el index.jsx de la aplicación.

Obtuve con éxito los datos de la base de datos, pero el resultado que obtengo en index.jsx siempre está indefinido.

El ARCHIVO API:

 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; });

}

El archivo index.jsx:

 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

No devuelve nada de la función getNotesFromDB , debe devolver el resultado de la llamada axios:

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

0

puedes simplemente devolver la promesa y manejar el 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 }) }

o

 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 })

o si prefiere usar 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