最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

Facebook Javascript SDK uncaught TypeError: Cannot read property 'userID' of undefined - Stack Overflow

matteradmin3PV0评论

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.

uncaught TypeError: Cannot read property 'userID' of undefined 

Code snippet

<div id="fb-root"></div>
<!-- Load the Facebook JavaScript SDK -->
<script src="//connect.facebook/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>

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.

uncaught TypeError: Cannot read property 'userID' of undefined 

Code snippet

<div id="fb-root"></div>
<!-- Load the Facebook JavaScript SDK -->
<script src="//connect.facebook/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>
Share Improve this question edited Dec 19, 2012 at 7:37 Joel Dean asked Dec 19, 2012 at 3:07 Joel DeanJoel Dean 2,4547 gold badges33 silver badges50 bronze badges
Add a ment  | 

3 Answers 3

Reset to default 4

If the app is not authorized, then the response object will not contain an authResponse property - hence the error you're seeing.

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;

here is the line of code which works for me (iOS and Android) :

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

userID was undefined for android, but userId is undefined for iOS.

Did you try response.authResponse.userId?

Post a comment

comment list (0)

  1. No comments so far