1

我有从 Linkedin 提取我的连接信息的工作代码。但是,每当我尝试拉位置时,我都会得到奇怪的结果。

此代码有效,但您会看到“object OBJECT”应该在哪里。

<html lang="en">
<head>
<title>LinkedIn Industries</title>
<script type="text/javascript" src="http://platform.linkedin.com/in.js">
api_key: xxxxxxx
onLoad: onLinkedInLoad
scope: r_network,r_emailaddress,r_fullprofile,r_basicprofile,r_contactinfo

</script>
</head>
<body>




<p>This example demonstrates how to retrieve a user's connections.  It also uses the LinkedIn auth events (load, login, logout) to control behavior.</p>

<!-- NOTE: be sure to set onLoad: onLinkedInLoad -->
<script type="text/javascript">
function onLinkedInLoad() {
  IN.Event.on(IN, "auth", function() {onLinkedInLogin();});
  IN.Event.on(IN, "logout", function() {onLinkedInLogout();});
}


function onLinkedInLogout() {
  setConnections({}, {total:0});
}

function onLinkedInLogin() {
  // here, we pass the fields as individual string parameters
  IN.API.Connections("me")
    .fields("id", "firstName", "lastName", "pictureUrl", "publicProfileUrl","location:(name)")
    .result(function(result, metadata) {
      setConnections(result.values, metadata);
    });
}

function setConnections(connections) {
  var connHTML = "<ul>";
  for (id in connections) {
    connHTML = connHTML + "<li><a href=\"" + connections[id].publicProfileURL + "\">";

    /* picture url not always there, must be defensive */
    if (connections[id].hasOwnProperty('pictureUrl')) {
      connHTML = connHTML + "<img align=\"baseline\" src=\"" + connections[id].pictureUrl + "\"></a>";
    }  else {
      connHTML = connHTML + "<img align=\"baseline\" src=\"http://static02.linkedin.com/scds/common/u/img/icon/icon_no_photo_80x80.png\"></a>";
    }

    connHTML = connHTML + "&nbsp;<a href=\"" + connections[id].publicProfileUrl + "\">";
    connHTML = connHTML + connections[id].firstName + " " + connections[id].lastName + "</a>";
    connHTML = connHTML + " (Location: " + connections[id].location + ")</li>";
  }

  connHTML = connHTML + "</ul>";
  document.getElementById("connectionsdata").innerHTML = connHTML;
}
</script>
<script type="IN/Login">
<div id="connectionstest">
  <p>Current User's Connections:</p>
  <div id="connectionsdata"></div>
</div>
</script>
</body>
</html>

主要问题确实存在于位置,在文档中这应该替换为 location.name,但是,每当我这样做时,我什么也得不到,尽管我可以看到有一个带有 json 的 XHR 响应,它具有:

位置:{“名称”:“都柏林等”}

请帮忙解开隐藏位置之谜

编辑:2013 年 5 月 23 日-在 Chrome 的开发人员工具中,我可以看到一条消息(未捕获的类型错误:无法读取未定义的属性“名称”-希望这对某人有所帮助

当我查看 chrome 开发人员的控制台时,有更多关于上述未捕获类型错误的详细信息: Uncaught TypeError: Cannot read property 'name' of undefined linkedconnect.html:55

setConnections linkedconnect.html:55

(匿名函数)linkedconnect.html:36 Sslac.Class.Constructor.Method.Method.Method.Method.Method.Method.Method.Method.Method.Method.Method.Method.Method.Method.Method.Method.Method.b框架:3242

(匿名函数)框架:173

Sslac.Class.Constructor.Method.Method.Method.Method.Method.Method.Method.Method.Method.Method.Method.Method.Method.Method.Method.Method.Method.Method.Method.storedFailureResults 框架:3247

(匿名函数)框架:173

g 框架:3135

pub.incoming 框架:801

_window_onMessage 框架:566

4

2 回答 2

1

由于错误消息建议您在实际尝试显示字符串时显示对象。因此,您需要选择该对象的正确属性:

connHTML = connHTML + " (Location: " + connections[id].location.name + ")</li>";
于 2013-05-22T14:15:28.260 回答
1

也许您可以将代码设置 html 临时替换为连接数组真正包含的内容的日志:

//connHTML = connHTML + "&nbsp;<a href=\"" + connections[id].publicProfileUrl + "\">";
//connHTML = connHTML + connections[id].firstName + " " + connections[id].lastName + "</a>";
//connHTML = connHTML + " (Location: " + connections[id].location + ")</li>";
console.log(connections[id]);

当您记录一个对象时,您可以单击它来检查它的属性,也许有些项目根本不包含位置。在这种情况下,您可以尝试:

function setConnections(connections) {
  var connHTML = "<ul>",
  tmpLocation;
  .....
  tmpLocation= (connections[id].location && connections[id].location.name)
    ?connections[id].location.name:"Unknown";
  connHTML = connHTML + " (Location: " + tmpLocation + ")</li>";
于 2013-05-23T09:35:13.153 回答