This content originally appeared on DEV Community and was authored by HAFIZ ABDULMANAN
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;
This content originally appeared on DEV Community and was authored by HAFIZ ABDULMANAN

HAFIZ ABDULMANAN | Sciencx (2022-07-20T09:55:33+00:00) ?Assigning Api resposne to a variable undefined error. Retrieved from https://www.scien.cx/2022/07/20/assigning-api-resposne-to-a-variable-undefined-error/
Please log in to upload a file.
There are no updates yet.
Click the Upload button above to add an update.