Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

221
Views
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 answers
Answer question

0

You should add await to async function

about 4 years ago · Juan Pablo Isaza Report

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!