Uncaught TypeError: FB.login is not a function

I use the JavaScript JavaScript API to create a login with a function using Facebook and get user information from the API.

When I use the same code in normal mode, I get data obtained from the Facebook api.

When I try to do the same with requireJS, I get this error

"Uncaught TypeError: FB.login is not a function"

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

Format i follow

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

and my fb.js code 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 html button code click

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

and JS code for 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"; } }); 
+1
source share
1 answer

The error message means that the JS SDK is not yet loaded. You are missing this 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 should be used AFTER FB.init . Make sure FB.init is FB.init .

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

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

+1
source

Source: https://habr.com/ru/post/1265340/


All Articles