Cyrille Armanger
Cyrille Armanger

Reputation: 759

Fetching results from LinkedIn API through javascript

First of all thank you for reading this. I am having some trouble fetching the data given by the Linkedin sign-in API with javascript. Here is the script:

    <script type="text/javascript">
    function onLinkedInAuth() {
    IN.API.Profile("me").fields(["firstName","lastName","headline","summary","location","educations","skills"]).result(displayProfiles);
    }

function displayProfiles(profiles) {
     member = profiles.values[0];
     document.getElementById("name").value = member.firstName +" "+ member.lastName;
     document.getElementById("pos").value = member.headline;
     document.getElementById("city").value = member.location.name;
     document.getElementById("sum").value = member.summary;

     var i=0; 
      do {

            var oldHTML = document.getElementById('para').innerHTML;
            var newHTML = oldHTML + "<tr><td>" + member.educations.values[i].schoolName + "</td></tr>";
            document.getElementById('para').innerHTML = newHTML;
        i++;
      }
      while(i<=1);
      var v=0;
      do {
            var oldHTML = document.getElementById('tara').innerHTML;
            var newHTML = oldHTML + "<tr><td>" + member.skills.values[v].skill.name + "</td></tr>";
            document.getElementById('tara').innerHTML = newHTML;
        v++; 
      }
      while(member.skills.values[v].skill.name);

     document.getElementById("educ").value = member.educations.values[1].schoolName;
     document.getElementById("skills").value = member.skills.values[0].skill.name;
}
</script>

It's a very basic script to get the user infos and, among it, the educational and professional background of the user. The thing is that member.educations.values[i].schoolName and member.skills.values[v].skill.name can have multiple values and I want to gather them all.

It works as long as the specified fields are not empty but then it outputs an error saying that member.skills.values[v] is undefined and it does not run the second loop.

I know the error is really basic but I'm not that great in javascript.

Thanks for your help anyways, have a good day!

Upvotes: 0

Views: 1689

Answers (1)

Unpossible
Unpossible

Reputation: 10697

You should check the length of the returned values and then loop through them as needed. Something along the lines of:

var educations = member.educations;
if(educations._total > 0) {
  for(var i = 0; i < educations._total; i++) {
    document.getElementById("educ").value += (i > 0) ? ', ' : '';
    document.getElementById("educ").value += educations.values[i].schoolName;
  }
}

Upvotes: 1

Related Questions