【问题标题】:Part of Facebook object is displaying as undefined, but it isn't undefinedFacebook 对象的一部分显示为未定义,但不是未定义
【发布时间】:2014-10-16 01:32:26
【问题描述】:

我正在处理来自 Facebook Javascript SDK 的结果,对象的“附件”部分给我带来了麻烦。查看对象时一切都很好,但是当我尝试在我的网站上显示它时,它说这些属性是未定义的。我是不是解析不正确?

这是对象:

{
"id":"xxx_xxx",
"from":{
    "id":"xxxx",
    "name":"User Name"
},
"to":{
    "data":[{
        "name":"Group Name",
        "id":"xxxx"
    }]
},
"picture":"url of image",
"link":"url",
"object_id":"Object ID",
"type":"photo",
"created_time":"2014-10-15T21:09:19+0000",
"updated_time":"2014-10-15T21:09:19+0000",
"attachments":{
    "data":[{
        "media":{
            "image":{
                "height":551,
                "src":"Image Source",
                "width":720
            }
        },
        "target":{
            "id":"742450405803347",
            "url":"target URL"
        },
        "type":"photo",
        "url":"URL"
    }]
}

这是在网站上显示“附件”部分的代码(我知道它很乱,请忽略):

post_div_html += '<p>------------------</p>\
                <p><b>Media-Image-Source</b>: ' + feed.attachments.data[0] + '</p>\
                <p><b>Target URL</b>: ' + feed.attachments.data[1] + '</p>\
                <p><b>Type</b>: ' + feed.attachments.data[2] + '</p>\
                <p><b>URL</b>: ' + feed.attachments.data[3] + '</p></div>';

现在,为了清楚起见,您在那里看到的结果是“未定义”在我的网站上显示为值。我尝试过的所有其他方法都导致错误提示“feed.attachments.data.whatever is undefined”。

有人对我需要如何展示这些东西有任何想法吗?

【问题讨论】:

  • 对于初学者来说,虽然feed.attachments.data 确实是一个数组,但它只有一个元素,所以你不能使用高于 0 的索引。其他任何东西(如你所拥有的 1、2 和 3在您的代码中)肯定是 undefined

标签: javascript object facebook-javascript-sdk facebook-graph-api-v2.0


【解决方案1】:

我想你想要的是……

var att = feed.attachments.data[0],
    mediaImageSrc = att.media.image.src,
    targetUrl = att.target.url,
    type = att.type,
    url = att.url;

我希望这是有道理的。

这是一个工作示例...

var feed = {
  "id": "xxx_xxx",
  "from": {
    "id": "xxxx",
    "name": "User Name"
  },
  "to": {
    "data": [{
      "name": "Group Name",
      "id": "xxxx"
    }]
  },
  "picture": "url of image",
  "link": "url",
  "object_id": "Object ID",
  "type": "photo",
  "created_time": "2014-10-15T21:09:19+0000",
  "updated_time": "2014-10-15T21:09:19+0000",
  "attachments": {
    "data": [{
      "media": {
        "image": {
          "height": 551,
          "src": "Image Source",
          "width": 720
        }
      },
      "target": {
        "id": "742450405803347",
        "url": "target URL"
      },
      "type": "photo",
      "url": "URL"
    }]
  }
};

var att = feed.attachments.data[0],
  mediaImageSrc = att.media.image.src,
  targetUrl = att.target.url,
  type = att.type,
  url = att.url;

post_div_html = '<p>------------------</p>\
                <p><b>Media-Image-Source</b>: ' + mediaImageSrc + '</p>\
                <p><b>Target URL</b>: ' + targetUrl + '</p>\
                <p><b>Type</b>: ' + type + '</p>\
                <p><b>URL</b>: ' + url + '</p></div>';
document.getElementById('out').innerHTML = post_div_html;
&lt;output id="out"&gt;&lt;/output&gt;

【讨论】:

    猜你喜欢
    • 2018-05-28
    • 1970-01-01
    • 1970-01-01
    • 2016-02-26
    • 1970-01-01
    • 2012-01-02
    • 2018-12-21
    • 1970-01-01
    • 2021-10-28
    相关资源
    最近更新 更多