Join the Stack Overflow Community
Stack Overflow is a community of 6.8 million programmers, just like you, helping each other.
Join them; it only takes a minute:
Sign up

I have tried many solutions For below mentioned error but nothing works for me I have refereed "https://developers.facebook.com/docs/javascript/howto/angularjs" for my implementation.

"Error: FB is not defined"

I am sharing my code below for controller and service files.

socialMedia.js -Controller

'use strict';
app
.controller('SocialMediaController', [
  '$rootScope', '$scope', '$location', '$cookieStore' , 'SocialMedia', 'ngToast','$window',
        function ($rootScope, $scope, $location, $cookieStore, SocialMedia, ngToast,$window) {

        $window.fbAsyncInit = function() {
            FB.init({ 
              appId: '************',
              status: true, 
              cookie: true, 
              xfbml: true,
              version: 'v2.4'
            });
        };

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

        $scope.testAPI = function() {
            SocialMedia.getMyLastName() 
             .then(function(response) {
               $scope.last_name = response.last_name;
            }
            );
        };

  }
]);

socialMedia.js -Services

   'use strict';
app
.factory('SocialMedia', [
    '$http', '$cookieStore', '$rootScope', 'appApi', function ($http, $cookieStore, $rootScope, appApi) {

        return {
            getMyLastName: function() {
                var deferred = $q.defer();
                FB.api('/me', {
                    fields: 'last_name'
                }, function(response) {
                    if (!response || response.error) {
                        deferred.reject('Error occured');
                    } else {
                        deferred.resolve(response);
                    }
                });
                return deferred.promise;
            }
        }
    }
]);

Html file

<input type="button" ng-click="testAPI()" value="Save" class="btn btn-primary md-trigger" />

Let me konw where I am wrong in this.

share|improve this question
    
just make sure that you are using FB.api AFTER FB.init. i assume your socialMedia service is calling FB.api before that. i see no login functionality either, btw. so /me will not work anyway. – luschn Nov 24 '16 at 9:37
    
maybe this helps you a bit: devils-heaven.com/facebook-javascript-sdk-login – luschn Nov 24 '16 at 9:37

Call your FB functions after the SDK is loaded asynchronously, Otherwise it will throw that "FB is not defined".

Put this code out side of AngularJs script.

// Load the SDK asynchronously
(function(d, s, id) {
      var js, fjs = d.getElementsByTagName(s)[   
      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'));

Code didn't test please test and update back.

share|improve this answer

Try removing

'use strict';

When u use that , you always have to declare FB before you use it. removing it use strict i think that you can solve this issue.

It is the best solution that i know.

Thank You Chathula

share|improve this answer
    
I have tried this not working for me. Thanks! – Sapna Mishra Nov 24 '16 at 8:12
    
sorry, but that´s completely wrong: developer.mozilla.org/docs/Web/JavaScript/Reference/Strict_m‌​ode – luschn Nov 24 '16 at 9:35

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.