当前位置: 动力学知识库 > 问答 > 编程问答 >

Facebook API (javascript) getting latest school education info

问题描述:

I'm very new to the facebook api for my website, and I am using the javascript sdk. I want to get the users latest school information, including school name, course and year of study. This is what I have so far but it breaks the login script and returns 'response.education.school is undefined'. I'm guessing I'll need some kind of for loop to go through the education array as most users have more than one school listed?

function login() {

FB.login(function(response) {

if(response.authResponse) {

// connected

FB.api('/me', function(response) {

fbLogin(response.id, response.name, response.firstname, response.email,

response.education.school.name, response.education.concentration.name, response.education.year.name);

});

} else {

// cancelled

}

}, {scope: 'email, user_education_history, user_hometown'});

}

网友答案:

response.education.school is undefined

This is because responce.education is an array of objects. This would be an example for me (actual information removed)

"education": [
    {
      "school": {
        "id": "", 
        "name": ""
      }, 
      "year": {
        "id": "", 
        "name": ""
      }, 
      "concentration": [
        {
          "id": "", 
          "name": ""
        }
      ], 
      "type": ""
    }, 
    ...
  ]

You need to iterate over it and process each educaional step e.g.

for(ed in response.education) {
   var school = response.education[ed].school;
   var schoolName = school.name;
   ...
}

And so on; you are currently passing an aobject structure to your fbLogIn that can't handle it. If you want the latest school education, you simply pick the one that has the most recent year.name value.

分享给朋友:
您可能感兴趣的文章:
随机阅读: