?Assigning Api resposne to a variable undefined error

hamsof

HAFIZ ABDULMANAN

Posted on July 20, 2022

?Assigning Api resposne to a variable undefined error

I am getting undefined for variable d; even console.log(res.data.results[0]) is working fine

import './App.css';
import a from 'axios'


function App() {
  let d;
  a.get("https://randomuser.me/api/")
  .then(res => {
    //console.log(res.data.results[0])
    d = res.data.results[0]
  })
  .catch(err => console.log("Its en error"))

  console.log(d) // undefined

  return (
    <div className="App">

    </div>
  );
}

export default App;
Enter fullscreen mode Exit fullscreen mode
💖 💪 🙅 🚩
hamsof
HAFIZ ABDULMANAN

Posted on July 20, 2022

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related