Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.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>
like image 924
Joel Dean Avatar asked Dec 19 '12 03:12

Joel Dean


3 Answers

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;
like image 133
Sean Kinsey Avatar answered Nov 12 '22 06:11

Sean Kinsey


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.

like image 35
dbaq Avatar answered Nov 12 '22 08:11

dbaq


Did you try response.authResponse.userId?

like image 24
Jens Peters Avatar answered Nov 12 '22 06:11

Jens Peters