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>
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?
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With