Facebook Javascript SDK uncaught TypeError: Unable to read property 'userID' from undefined

I am trying to implement an authorization function using the JavaScript JavaScript SDK. When I run it, it checks the console, I see an 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 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> 
+8
javascript facebook-javascript-sdk
source share
3 answers

If the application is not authorized, the response object will not contain the authResponse property - therefore, you will see an error.

Do you want to

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

or simply

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

or simply

 uid = response.authResponse && response.authResponse.userID; 
+4
source share

Have you tried to answer response.authResponse.userId?

+1
source share

here is a line of code that works for me (iOS and Android):

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

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

+1
source share

All Articles