0

I get error FB not defined when using Facebook Javascript SDK

FB.init({
    appId: '{APP ID}',
    status: true,
    cookie: true,
    xfbml: true
});

Jsfiddle

1 Answer 1

6

Two things-

  1. I can't see the JS SDK been loaded.

  2. You have to call your FB functions after the SDK is loaded asynchronously, else it will throw that FB is not defined

So, to fix it-

// This will be triggered automatically after the SDK is loaded successfully
// write your FB fucntions inside this
window.fbAsyncInit = function() {
   FB.init({
     appId: '{app-id}',
     status: true,
     cookie: true,
     xfbml: true
   });

   FB.getLoginStatus(function (response) {
     if (response.status === 'connected') {
        GetData();
     } else {
        Login();
     }
  });
};

// JS SDK - this will be loaded asynchronously
(function(d, s, id){
  var js, fjs = d.getElementsByTagName(s)[0];
  if (d.getElementById(id)) {return;}
  js = d.createElement(s); js.id = id;
  js.src = "//connect.facebook.net/en_US/all.js";
  fjs.parentNode.insertBefore(js, fjs);
}(document, 'script', 'facebook-jssdk'));

function Login() {
   ...
   ...
}

function GetData() {
   ...
   ...
}

Reference

Edit:

I saw that you are requesting for the permission email and yet not using it. Use the actual email (not the facebook one) with- response.email

Sign up to request clarification or add additional context in comments.

11 Comments

I have updated the jsfiddle still I see the function window.fbAsyncInit is not called
I am listing facebook friends email id. It does not show anything
I'm getting the result in console. Aren't you getting anything in console? not even any error? Another thing- use that with a valid url, you are using the jsfiddle url which is not configured in the app settings, so you must be getting error: "Giver URL is not ....."
No I am not getting any result in console. fbAsyncInit function is not called. I put an alert and tested. It is not called at all
In fiddle i can see error "Given URL is.." because in app settings you have not configured the site url to jsfiddle.....
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.