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

224
Views
console.log() prints data to console but return data returns [object Promise] (ReactJS | Axios)

I'm hoping someone could help me with this project:

I'm calling a function that uses axios to get data from an API, I keep getting [object Promise].

I tried: getAPIdata().then(function(data){console.log(data)}) and I get the data correctly printed on the console. But what I need, is to return the data so it can be mapped.

I tried:

getAPIdata().then(function(data){return data}) but this simply returns [object Promise]

which is the same thing I get if I simply do:

getAPIdata() it also returns [object Promise]

Does anyone know how I can get the data returned properly? For reference, I've added the code samples below.

Please let me know. Thanks.

getAPIdata = async () => {
   const axios = require('axios').default;
   try{
      return await axios.get('URL').then(content => content.data);
   } catch (error){
      throw{
         code: error.code,
         message: error.message,
         responseStatus: error.response?.status, 
         url
      };
   }
}    
getAPIdata().then(function(data){console.log(data)})  // returns data to console
getAPIdata().then(function(data){return data})   // returns [object Promise]

Update: getAPIdata().then(function(data){return data}) is then passed as a prop to another component. the prop is called selection

function Select(props){
   const {selection} = props;
   return(
      <Select>
         {console.log(typeof selection)} //returns object
         <MenuItem value="">None</MenuItem> //Default value
         {
            **selection.map(
            data =>(<MenuItem key={data.id} value={data.id}>{data.name}</MenuItem>)**
            )
         }

      </Select>
   )

Ultimately I get that selection.map is not a function, which is what led me to assume this is an issue with the data not being returned properly. The Select and MenuItem components are from Material-UI.

about 4 years ago · Juan Pablo Isaza
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!