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

223
Vistas
I am using axios to get data from a simple https api but it returns empty

but when i go on the that link there's json data.

import axios from "axios";
exports.handler = async function (event, context) {
  let results;
  const fetchData = async () => {
    axios.get("https://randomuser.me/api").then((res) => {
      console.log(res);
    });
    // conlog.log(results.data);
  };

  return {
    statusCode: 200,
    body: JSON.stringify(fetchData()),
  };
};

here's my code i tried to read this with python requests and it worded fine.

i even tried

import axios from "axios";
exports.handler = async function (event, context) {
  return {
    statusCode: 200,
    body: JSON.stringify(axios.get("https://randomuser.me/api")),
  };
};

but it still returns an {}. can someone please tell me what am doing wrong here.

Edit:

i at first had this:

import axios from "axios";
exports.handler = async function (event, context) {
  let results;
  const fetchData = async () => {
    results = await axios.get("https://randomuser.me/api");
    conlog.log(results.data);
    return results.data;
  };

  return {
    statusCode: 200,
    body: JSON.stringify(fetchData()),
  };
};

Edit 2:

i Have this code below as part of my app.js and it works just fine.

 const fetchData = async () => {
    const results = await axios.get("/.netlify/functions/print");
    console.log(results);
  };

  useEffect(() => {
    fetchData();
  }, []);
about 4 years ago · Juan Pablo Isaza
2 Respuestas
Responde la pregunta

0

You should add await to async function

about 4 years ago · Juan Pablo Isaza Denunciar

0

You should add await to the fetchData async function. And remove async from the parent function

import axios from "axios";
exports.handler = function (event, context) {
  let results;
  const fetchData = async () => {
   await axios.get("https://randomuser.me/api").then((res) => {
      console.log(res);
    });
    // conlog.log(results.data);
  };

  return {
    statusCode: 200,
    body: JSON.stringify(fetchData()),
  };
};
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