javascript 未捕获的类型错误:无法读取未定义的属性“名称”
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15312479/
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
Uncaught TypeError: Cannot read property 'name' of undefined
提问by Ido
I tried to fetch data using facebook api into form inputs. Now, everything went just fine until I tried to also fetch the location of the current user.
我尝试使用 facebook api 将数据提取到表单输入中。现在,一切都很好,直到我还尝试获取当前用户的位置。
If the current user have shared his location (Where he is living), then I get no problems. However, if the user hasn't shared his location on facebook, I am getting an error:
Uncaught TypeError: Cannot read property 'name' of undefined
如果当前用户共享了他的位置(他住的地方),那么我不会遇到任何问题。但是,如果用户没有在 facebook 上分享他的位置,我会收到一个错误:
Uncaught TypeError: Cannot read property 'name' of undefined
Here is the code I've been using. If you have any idea how to solve it, please comment here :)
这是我一直在使用的代码。如果您有任何想法如何解决它,请在此处发表评论:)
<div id="fb-root"></div>
<script>
// Additional JS functions here
window.fbAsyncInit = function() {
FB.init({
appId : '*******', // App ID
status : true, // check login status
cookie : true, // enable cookies to allow the server to access the session
xfbml : true // parse XFBML
});
FB.getLoginStatus(function(response) {
if (response.status === 'connected') {
FB.api('/me', function(response) {
document.getElementById("user_login").value="FB - " + response.username;
document.getElementById("user_email").value=response.email;
document.getElementById("first_name").value=response.first_name;
document.getElementById("last_name").value=response.last_name;
document.getElementById("user_url").value=response.link;
document.getElementById("fbid").value=response.id;
if(response.location !== 'undefined')
{
document.getElementById("location").value=response.location.name;
alert(response.location.name);
}
document.getElementById("registerform").submit();
});
} else if (response.status === 'not_authorized') {
alert('error');
} else {
alert('Please Log In!');
}
});
};
// Load the SDK Asynchronously
(function(d){
var js, id = 'facebook-jssdk', ref = d.getElementsByTagName('script')[0];
if (d.getElementById(id)) {return;}
js = d.createElement('script'); js.id = id; js.async = true;
js.src = "//connect.facebook.net/en_US/all.js";
ref.parentNode.insertBefore(js, ref);
}(document));
</script>
回答by T.J. Crowder
On this line:
在这一行:
if(response.location !== 'undefined')
...you're checking if response.location
is not the string"undefined"
. Since undefined
is not the string "undefined"
, the condition is true. Then you try to use response.location.name
and as response.location
is undefined
, you get the error.
...您正在检查是否response.location
不是string"undefined"
。由于undefined
不是字符串"undefined"
,条件为真。然后,您尝试使用response.location.name
和response.location
是undefined
,你的错误。
You probably meant either:
你可能的意思是:
if(response.location)
or
或者
if(typeof response.location !== 'undefined')