I am aware that receiving the following error is a common problem:
ReferenceError:FB is not defined
I have referenced the following post which seems to cover a wide array of solutions. FB is not defined problem
The problem specifically I am having is that if I test out my app deploying to the browser, the fbAsyncInit(...)
is hit in my root index.html
page.I know this because I put an alert('fbAsyncInit(..)')
on the first line of the FB init code. For example, if I run the following command, It alerts fbAsyncInit(..)
ionic serve
As soon as I deploy to a device using the following command, I get no alert and when I try to actually invoke $cordovaFacebook.login(...)
it gives me the following error:
ReferenceError: FB is not defined
My root index.html
looks similar (removed some stuff for brevity).
<html>
<!-- Other stuff here -->
<div id="fb-root"></div>
<script type="text/javascript" src="lib/additional/FacebookConnectPlugin.js"></script>
<script>
window.fbAsyncInit = function() {
console.log('fbAsyncinit(..)');
FB.init({
appId : '/*App id here*/',
cookie : true, // enable cookies to allow the server to access
xfbml : true, // parse social plugins on this page
version : 'v2.1' // use version 2.1
});
(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>
<!-- Other Stuff Here -->
</html>
The one thing it is hitting is the following piece of code defined in FacebookConnectPlugin.js
// Bake in the JS SDK
(function () {
if (!window.FB) {
console.log("launching FB SDK");
var e = document.createElement('script');
e.src = document.location.protocol + '//connect.facebook.net/en_US/sdk.js';
e.async = true;
document.getElementById('fb-root').appendChild(e);
console.log(window.FB) //FB is undefined
}
}());
When the script tag loads for FacebookConnectPlugin.js
and this piece of code runs, it attempts to print window.FB
to the console, but its undefined.
Any idea what is wrong given the code above?