skip to Main Content

I am Using Facebook JavaScript API to create login with feature with Facebook and fetch the user details from the API.

When I am using the same code in normal way, I am getting the data fetched from from Facebook api.

When I try to do the same thing with requireJS, I am Getting this error

“Uncaught TypeError: FB.login is not a function”

I have seen errors like undefined function but this new to me.

The format which I am following

https://developers.facebook.com/docs/javascript/howto/requirejs/v2.7

and my code fb.js goes here

define(['jquery','facebook'], function($,FB){

window.fbAsyncInit = function() {
    FB.init({
        appId   : '89080988080545753578',
        oauth   : true,
        status  : true, // check login status
        cookie  : true, // enable cookies to allow the server to access the session
        xfbml   : true ,// parse XFBML
        version    : 'v2.5' 
    });

  };

window.fb_login = function(){
FB.login(function(response) {

        if (response.authResponse) {
            console.log('Welcome!  Fetching your information.... ');
            //console.log(response); // dump complete info
            access_token = response.authResponse.accessToken; //get access token
            user_id = response.authResponse.userID; //get FB UID

            FB.api('/me', function(response) {
                user_email = response.email; //get user email
                $("#name").val(response.name);
                $("#user-id").val(response.id);
                if(response.gender == 'male') {
                    $('input[name="gender"][value="female"]').attr('checked', false);
                    $('input[name="gender"][value="male"]').attr('checked', true);

                } else {

                    $('input[name="gender"][value="male"]').attr('checked', false);
                    $('input[name="gender"][value="female"]').attr('checked', true);
                }


                $("#birthday").val(response.birthday);
                $("#email").val(response.email);
          // you can store this data into your database             
            });

        } else {
            //user hit cancel button
            console.log('User cancelled login or did not fully authorize.');

        }

    }, {
        scope: 'publish_stream,email'
    });
};
 });

and the html code for the button click

<button type="button" class="btn btn-lg btn-block sm fb" value="Login" id="FB">Facebook</button>

and JS code for the click event

$( document ).ready(function() {
    $(".btn").click(function(){
        //getting button id value in variable
        var method = $(this).attr('id');
        $("#sign_method").val(method);
        if(method == 'FB') {

            alert('Facebook');
            fb_login();
            //fbData();
            //$("#signup").submit();

        } else if(method == 'GP') {

            alert('Google Plus');
            login();
           // $("#signup").submit();

        } else {

            alert('Email');               
            $("#signup").remove();
            alert('form removed');

            window.location.href = "signup.html";

        }
    });

2

Answers


  1. The error message means that the JS SDK is not loaded yet. You are missing this in your code:

    (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'));
    

    FB.login must be used AFTER FB.init. Make sure FB.init gets called.

    More information: http://www.devils-heaven.com/facebook-javascript-sdk-login/

    For RequireJS, this would be the official way to include the JS SDK: https://developers.facebook.com/docs/javascript/howto/requirejs/v2.7#adding-a-shim-to-the-facebook-sdk

    Login or Signup to reply.
  2. In my case, I had an adblocker extension that was preventing SDK from initializing properly. I only realized after trying in a different browser that didn’t have adblocker extension

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search