Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

FB is not defined javascript

I've a problem with FB JS SDK.

I'm trying to do a request to get the fan_count of a facebook page node.

Here is my code from my html file into the body :

<script>
  window.fbAsyncInit = function() {
      FB.init({
        appId      : 'your-app-id',
        xfbml      : true,
        version    : 'v2.5'
      });
    };

    (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/sdk.js";
      fjs.parentNode.insertBefore(js, fjs);
    }(document, 'script', 'facebook-jssdk'));
</script>

And when I'm using this on my js app, I use it :

init();

function init() {
  var id_fb  = "l214.animaux";
  while (true) {
    console.log("je suis ici");
    FB.api(
      '/' + id_fb +  '/',
      'GET',
      {"fields":"fan_count"},
      function(response) {
        alert(response.fan_count);
      }
    );
  }
}

But the error is that FB is not defined. Any suggestions ?

like image 489
Couim Avatar asked Apr 30 '16 14:04

Couim


2 Answers

This would be correct, you need to use FB after the JS SDK is initialized. That being said, you definitely don´t want to call FB.api in an infinite loop, so i removed that part:

<script>
    function init() {
        FB.api(
          '/l214.animaux',
          {"fields":"fan_count"},
          function(response) {
            alert(response.fan_count);
          }
        );
    }

    window.fbAsyncInit = function() {
      FB.init({
        appId      : 'your-app-id',
        xfbml      : true,
        version    : 'v2.5'
      });

      init();
    };

    (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/sdk.js";
      fjs.parentNode.insertBefore(js, fjs);
    }(document, 'script', 'facebook-jssdk'));
</script>

Make sure you run this from an actual server, don´t just open your HTML files in a browser without at least a local server.

like image 188
andyrandy Avatar answered Nov 14 '22 08:11

andyrandy


I got this error because I write the init code in an independent js file, so, of course FB is not defined, because it should be window.FB.

my code:

class FacebookUtil {

  static init() {
    // comes from https://developers.facebook.com/docs/javascript/quickstart
    // notice FB should be window.FB
    window.fbAsyncInit = function() {
      window.FB.init({
        appId            : '...',
        autoLogAppEvents : true,
        xfbml            : true,
        version          : 'v2.10'
      });
      window.FB.AppEvents.logPageView();
    };

    (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/sdk.js";
      fjs.parentNode.insertBefore(js, fjs);
    }(document, 'script', 'facebook-jssdk'));
  }

  static login() {
    window.FB.login(...)
  }

}
like image 40
Spark.Bao Avatar answered Nov 14 '22 08:11

Spark.Bao