无法读取未定义错误 Javascript 的属性
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17152091/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Cannot read property of undefined error Javascript
提问by Precastic
Getting an error when trying to retrieve the amount of people someone is followed_by on Instagram.
尝试检索某人在 Instagram 上被关注的人数时出错。
The API is getting called properly but in line 10 I am getting an error "Uncaught TypeError: Cannot read property 'followed_by' of undefined".
API 被正确调用,但在第 10 行我收到错误“未捕获的类型错误:无法读取未定义的属性 'followed_by'”。
Code is below.
代码如下。
function hello() {
var $url = 'https://api.instagram.com/v1/users/7624/?access_token={access-token}&count=100';
var $access_token = "257177111.ca91fd6.d912fbc4875d4d81abe28ee7b436d8da";
$.ajax({
method: "GET",
url: $url,
dataType: "jsonp",
jsonp : "callback",
success: function(data) {
alert(data.counts.followed_by);
},
error: function(data, error) {
alert("bad");
}
});
}
回答by Precastic
Change this:
改变这个:
alert(data.counts.followed_by);
to
到
alert(data);
alert(data.counts);
alert(data.counts.followed_by);
The first one that comes up as undefined is the one that doesn't exist & then fix your problem from there.
出现未定义的第一个是不存在的,然后从那里解决您的问题。
If is a whole lot easier to use console.log() to find these sorts of problems because you can just do console.log(data)
& it gives you the whole object which you can inspect using any of the available dev tools used for that.
如果使用 console.log() 来查找这些类型的问题要容易得多,因为您可以这样做console.log(data)
并且它为您提供了整个对象,您可以使用任何可用的开发工具进行检查。