Facebook Javascript SDK 未捕获 TypeError:无法读取未定义的属性“userID”
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13944940/
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
Facebook Javascript SDK uncaught TypeError: Cannot read property 'userID' of undefined
提问by Joel Dean
I am trying to implement an authorization feature using the Facebook JavaScript SDK. When I run it, it and check the console I see the error.
我正在尝试使用 Facebook JavaScript SDK 实现授权功能。当我运行它时,它并检查控制台我看到了错误。
uncaught TypeError: Cannot read property 'userID' of undefined
Code snippet
代码片段
<div id="fb-root"></div>
<!-- Load the Facebook JavaScript SDK -->
<script src="//connect.facebook.net/en_US/all.js"></script>
<script>
var appId = 'APP_ID';
var uid;
// Initialize the JS SDK
FB.init({
appId: '413026618765431',
cookie: true,
});
// Get the user's UID
FB.getLoginStatus(function(response) {
uid = response.authResponse.userID ? response.authResponse.userID : null;
});
function authUser() {
FB.login(function(response) {
uid = response.authResponse.userID ? response.authResponse.userID : null;
}, {scope:'email,publish_actions'});
}
</script>
采纳答案by Sean Kinsey
If the app is not authorized, then the response object will notcontain an authResponse
property - hence the error you're seeing.
如果应用程序未经授权,则响应对象将不包含authResponse
属性 - 因此您会看到错误。
What you want is
你想要的是
uid = response.authResponse
? response.authResponse.userID
: null;
or simply
或者干脆
uid = response.authResponse && response.authResponse.userID || null;
or simply
或者干脆
uid = response.authResponse && response.authResponse.userID;
回答by dbaq
here is the line of code which works for me (iOS and Android) :
这是对我有用的代码行(iOS 和 Android):
var uid = response.authResponse.userID || response.authResponse.userId;
var uid = response.authResponse.userID || response.authResponse.userId;
userID was undefined for android, but userId is undefined for iOS.
android 的 userID 未定义,但 iOS 的 userId 未定义。
回答by Jens Peters
Did you try response.authResponse.userId?
你试过 response.authResponse.userId 吗?