未捕获的类型错误:不能读取属性'name'的定义
Uncaught TypeError: Cannot read property 'name' of undefined
我尝试使用facebook api获取数据到表单输入。现在,一切都很顺利,直到我试图获取当前用户的位置。
如果当前用户共享了他的位置(他住在哪里),那么我没有问题。然而,如果用户没有在facebook上分享他的位置,我得到一个错误:Uncaught TypeError: Cannot read property 'name' of undefined
这是我一直使用的代码。如果你知道如何解决这个问题,请在这里评论:)
<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>
这一行:
if(response.location !== 'undefined')
…您正在检查response.location
是否不是字符串 "undefined"
。因为undefined
不是字符串"undefined"
,所以条件为真。然后你尝试使用response.location.name
,因为response.location
是undefined
,你得到错误。
你的意思可能是:
if(response.location)
或
if(typeof response.location !== 'undefined')
相关文章:
- 使用方法和属性定义javascript对象的最佳方法是什么?(如果可能以本机方式)
- Javascript - 使用数组属性定义对象
- ECMAScript 中的内部属性定义是为了什么
- 为什么不在对象外部使用属性定义语法是语法错误
- ES6 类属性定义
- 为javascript类的属性定义函数
- 属性定义是使用敲除时的方法定义
- 为AngularJS的子属性定义getter setter
- 如何给顶部属性定义在CSS chrome或firefox
- 如何通过两个属性定义元素
- 为什么函数属性定义在对象本身上(例如Function.name),而方法定义在Function.prototype上?
- Javascript原型属性定义为对象
- 停止传播在ASP.Net中使用属性定义的JavaScript事件
- 用不同的属性定义同一个Grunt Task两次
- AngularJS:在隔离范围内获取属性定义指令的值
- 需要另一个使用属性定义控制器的指令的角度指令
- 基于其他属性定义对象属性值
- 如何将属性定义为变量
- 奇怪的javascript属性定义使用聚合物
- fabric.js -如果属性定义在new fabric.Circle()括号之外,则Circle不起作用