8

我正在尝试使用 Facebook JavaScript SDK 实现授权功能。当我运行它并检查控制台时,我看到了错误。

uncaught TypeError: Cannot read property 'userID' of undefined 

代码片段

<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>
4

3 回答 3

4

如果应用程序未经授权,则响应对象将包含authResponse属性 - 因此您会看到错误。

你想要的是

uid = response.authResponse 
  ? response.authResponse.userID 
  : null;

或者干脆

uid = response.authResponse && response.authResponse.userID || null;

或者干脆

uid = response.authResponse && response.authResponse.userID;
于 2012-12-19T04:07:39.023 回答
2

这是适用于我的代码行(iOS和Android):

var uid = response.authResponse.userID || response.authResponse.userId;

android 的 userID 未定义,但 iOS 的 userId 未定义。

于 2013-09-21T03:18:41.367 回答
1

您是否尝试过 response.authResponse.userId?

于 2013-03-08T20:31:17.587 回答