2017-04-08 121 views
0

我想从Facebook发布帖子和附件,所以我可以创建一个帖子列表。问题是,我想把属性:ID,消息和任何附件放在数组中,以便为每个帖子提供。我不确定是否需要在数组中迭代对象,或者我可以选择ID并获取相关信息。Node.js通过嵌套的Javascript对象循环

此外,我正在使用async.waterfall()来获取同步呼叫,因为我会进行其他调用,这取决于以前的调用。

我想这让附件的ID,消息和src网址:

var async = require("async"); 
var graph = require('fbgraph'); 

async.waterfall([ 
    getFeed, 
    getPostPicture, 
], function (err, result) { 
}); 
function getFeed(callback) { 
    graph.get("/.../feed" + "?fields=message,attachments&access_token=APPID|APPSECRET", function(err, res) { 
     callback(null, res); 
    }); 
} 
function getPostPicture(arg1, callback) { 

    var feedData = arg1.data; 
    for(var i in feedData) 
    { 
     var id = feedData[i].id; 
     var message = feedData[i].message; 
     var feedAttachment = feedData[i].attachments 

     for (var j in feedAttachment) 
     { 
      var attachment = feedAttachment.data; 
      var attachmentURL = attachment[i].src; 
      for (var j in attachmentURL) 
      { 
       var attachmentURL = feedAttachment.src; 
      } 
     } 
    } 
     console.log(attachment); 
} 

上面会输出:

[ { description: 'post message 1', 
    media: { image: [Object] }, 
    target: 
    { id: '...', 
     url: 'https://www.facebook.com/.../photos/a............/.../?type=3' }, 
    title: 'Timeline Photos', 
    type: 'photo', 
    url: 'https://www.facebook.com/.../photos/a............/.../?type=3' } ] 

下面是我第一次叫graph.get在响应源代码

,我需要{数据 - > [消息,ID,附件 - >数据[SRC]]}

{ 
    "data": [ 
    { 
     "message": "post message 1", 
     "id": "..._...", 
     "attachments": { 
     "data": [ 
      { 
      "description": "picture 1", 
      "media": { 
      "image": { 
      "height": 256, 
      "src": "https://scontent.xx.fbcdn.net/v/t1.0-9/..._..._..._n.png?oh=...&oe=...", 
      "width": 256 
      } 
     }, 
     "target": { 
      "id": "...", 
      "url": "https://www.facebook.com/.../photos/a............/.../?type=3" 
     }, 
     "title": "Timeline Photos", 
     "type": "photo", 
     "url": "https://www.facebook.com/.../photos/a............./..../?type=3" 
     } 
     ] 
    } 
    }, 
    { 
     "message": "Test status update 123", 
     "id": "..._..." 
    } 
    ], 
    "paging": { 
    "previous": "https://graph.facebook.com/v2.8/.../feed?fields=message,attachments&format=json&since=...&access_token=...&limit=25&__paging_token=enc_...&__previous=1", 
    "next": "https://graph.facebook.com/v2.8/.../feed?fields=message,attachments&format=json&access_token=...&limit=25&until=...&__paging_token=enc_..." 
    } 
} 
+0

那么'console.log(附件);'net你输出的是什么?你确定你的代码粘贴正确吗?请展开您的代码以更好地显示在哪个点生成哪个输出。 – xDreamCoding

回答

0

当使用该用于getPostPicture功能循环中,变量i实际上是阵列feedData在元件,所以你必须i.idi.attachmentsi.message。此外,我认为,因为您在两个循环中使用相同的j,并且一个嵌套在另一个循环中,所以它也不能正常工作,因此您可能还必须更改,并解释您对所期望的输出类型的问题。