2012-04-19 45 views
1

我正在使用Facebook Connect爲我的網站開發一個新Facebook應用程序。我正在使用JS SDK,並嘗試顯示用戶的名稱。這裏是我的代碼:FB.api name undefined

window.fbAsyncInit = function() { 
    FB.init({ 
     appId  : 'myappid', // App ID 
     oauth  : 'true', 
     channelUrl : '//mychannelfile', // Channel File 
     status  : true, // check login status 
     cookie  : true, // enable cookies to allow the server to access the session 
     xfbml  : true // parse XFBML 
    }); 

    FB.Event.subscribe('auth.login', function (response) { 
       // do something with response 

      }); 

    var connecter=false; 

    FB.getLoginStatus(function(response) { 
      if (response.status === 'connected') { 
       connecter=true; 
       FB.api('/me', function(user) { 
       console.log(user.name); 
       }); 
      } 
     else connecter=false; 
     }); 
    }; 

如果我檢查控制檯的用戶連接時user.name的結果是不確定的。我不明白我做錯了什麼。

謝謝你的幫助!

+1

你有沒有試過檢查console.log(response)對象和console.log(用戶)對象? – Dhiraj 2012-04-19 20:32:31

+0

我不明白爲什麼,但是當我在console.log(user.name)之前添加console.log(user)時,它工作正常...... – Kevin 2012-04-19 20:36:39

+0

這意味着您在控制檯的用戶對象中看到名稱屬性? – Dhiraj 2012-04-19 20:43:05

回答

0
window.fbAsyncInit = function() { 
// init the FB JS SDK 
FB.init({ 
    appId  : '176854392480453',       
    status  : true,         
    xfbml  : true         
}); 

// Additional initialization code such as adding Event Listeners goes here 

FB.getLoginStatus(function(response) { 
    if (response.status === 'connected') { 
alert("connected"); 
connecter=true; 
FB.api('/me', function(user) { 
alert(user.name); 
alert(user.first_name); 
alert(user.last_name); 
alert(user.email); 
}); 

    } 
});