admin管理员组

文章数量:1332389

So, I am just learning about axios and everything looks right, but I suppose I'm not understanding something about global scopes. Here is my code:

axios.get('')
.then(function (response) {
console.log('Response: ', response.data);
const data = response.data;
return data;
})
.catch(function (handleError) {
console.log('Error: ', handleError);
},[]);

const myData = function(data) {

name = data.name; // This is the line with the error: data is not defined. But it is, isn't it?
return name;
}

console.log(myData(data));

So, I am just learning about axios and everything looks right, but I suppose I'm not understanding something about global scopes. Here is my code:

axios.get('https://api.github./users/social-collab')
.then(function (response) {
console.log('Response: ', response.data);
const data = response.data;
return data;
})
.catch(function (handleError) {
console.log('Error: ', handleError);
},[]);

const myData = function(data) {

name = data.name; // This is the line with the error: data is not defined. But it is, isn't it?
return name;
}

console.log(myData(data));
Share Improve this question edited Jun 5, 2020 at 17:09 Donald Faulknor asked Jun 5, 2020 at 17:04 Donald FaulknorDonald Faulknor 572 silver badges7 bronze badges 3
  • Here, in your code you've defined const data in your axios API response, so the scope will be block level i.e., it'll be accessible only inside then(). That's why you are getting error. What exactly you are trying to do? – Nithish Commented Jun 5, 2020 at 17:10
  • Ok, I'll try that. Axios confuses me because the whole axios isn't enclosed in braces: axios.get() { /* .then and .catch */ } so it makes me ask, how does it know the variable data would be associated to axios? It just looks different from all the other JavaScript I've learned in the past 5 weeks at school. – Donald Faulknor Commented Jun 5, 2020 at 17:13
  • Ok, I tried that and got what I thought would happen. Now, const data = response.data has "response is not defined" and that's because it's not within the .then. – Donald Faulknor Commented Jun 5, 2020 at 17:17
Add a ment  | 

2 Answers 2

Reset to default 6

You don't need a global scope, just chain another .then with your function myData.

const myData = function(data) {
  name = data.name;
  console.log('name:', name)
  return name;
}

axios.get('https://api.github./users/social-collab')
.then(response => {
  const data = response.data;
  return data
})
.then(myData)
.catch(err => {
  console.log('Error: ', err);
})
<script src="https://cdn.jsdelivr/npm/axios/dist/axios.min.js"></script>

The way to get data out of Promises is by supplying a function to .then.

If you return something from an async function (promise which will get your data from somewhere else) then you won't get the return value immediately. You can check my solution to understand better.

function getData() {
  axios
    .get("https://api.github./users/social-collab")
    .then(function (response) {
      console.log("Response: ", response.data);
      return response.data;
    })
    .catch(function (handleError) {
      console.log("Error: ", handleError);
    }, []);
}

const myData = function (data) {
  name = data.name;
  console.log(name);
};

getData().then((data) => {
  myData(data);
});
<script src="https://cdnjs.cloudflare./ajax/libs/axios/0.19.2/axios.min.js"></script>

本文标签: javascriptHow can I make a global variable in axiosStack Overflow