Search code examples
javascriptfacebookweb-applicationsmobile-safari

Facebook FB.api( '/me' ) throwing: "TypeEror: 'undefined' is not an object"


Facebook's FB.api( '/me' ) function recently started throwing the following error when run in mobile Safari:

TypeEror: 'undefined' is not an object

The code works fine in a desktop browser (tested in Chrome 19dev/OS X and Safari 5.1.3/OS X). I have tried two different Facebook app IDs on two different domains with the same results. I have tried two different Facebook user IDs with the same results. I have tried authenticating with FB.login() and FB.getLoginStatus() and it made no difference.

Demo: http://thinkingstiff.com/fb.htm

Script:

window.onload = function () {

    var script = document.createElement( 'script' );
    script.async = true;
    script.src = document.location.protocol + '//connect.facebook.net/en_US/all.js';
    document.getElementById( 'fb-root' ).appendChild( script );

    window.fbAsyncInit = function () {
        FB.init( {
            appId: '252706924818818',
            cookie: true,
            status: true,
            xfbml: true,
            oauth: true
        } );
    };

    function login1() {
        FB.login( function ( response ) {
            if ( response.authResponse ) {
                try {
                    FB.api( '/me', function ( response ) { // this line throws error
                        alert( 'success: ' + response.name );
                    } );
                } catch ( error ) {
                    alert( 'error: ' + error );
                };
            } else {
                alert( 'unauthorized' );
            };
        }, { scope: 'email' } );
    };

    function login2() {
        FB.getLoginStatus( function ( response ) {
            if ( response.authResponse ) {
                try {
                    FB.api( '/me', function ( response ) { // this line throws error
                        alert( 'success: ' + response.name );
                    } );
                } catch ( error ) {
                    alert( 'error: ' + error );
                };
            } else {
                alert( 'unauthorized' );
            };
        } );
    };

    function initialize() {
        document.getElementById( 'login1' ).addEventListener( 'click', login1, false );
        document.getElementById( 'login2' ).addEventListener( 'click', login2, false );
    };

    initialize();

};
​

HTML:

<button id="login1" class="fb-button">Login 1</button>
<button id="login2" class="fb-button">Login 2</button>
<div id="fb-root"></div>

Solution

  • It looks like FB pushed some changes yesterday that might've caused this problem. Any browser with Flash deactivated seems to cause api calls to break. I am even having trouble logging into Spotify.com on iphone / ipad Safari.

    This looks like the bug reported at: http://developers.facebook.com/bugs/315958608459599?browse=search_4f60cb168df304e98947536