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 youraxios
API response, so the scope will be block level i.e., it'll be accessible only insidethen()
. 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
2 Answers
Reset to default 6You 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
版权声明:本文标题:javascript - How can I make a global variable in axios? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742289162a2447479.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论