[英]Uncaught TypeError: Cannot read property 'name' of undefined
I tried to fetch data using facebook api into form inputs. 我尝试使用facebook api将数据提取到表单输入中。 Now, everything went just fine until I tried to also fetch the location of the current user. 现在,一切都很顺利,直到我试图获取当前用户的位置。
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>
On this line: 在这一行:
if(response.location !== 'undefined')
...you're checking if response.location
is not the string "undefined"
. ...你正在检查response.location
是不是字符串 "undefined"
。 Since undefined
is not the string "undefined"
, the condition is true. 由于undefined
不是字符串"undefined"
,因此条件为真。 Then you try to use response.location.name
and as response.location
is undefined
, you get the error. 然后你尝试使用response.location.name
并且由于response.location
undefined
,你会收到错误。
You probably meant either: 你可能意味着:
if(response.location)
or 要么
if(typeof response.location !== 'undefined')
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.