Saturday 8 April 2017

Node.js loop through nested Javascript object

I want to have posts + attachments from Facebook so I can create a list of posts. The problem is that I want to put attributes: id, message and any attachment in a array for every post feed. I'm not sure whether I need to iterate objects inside a array or can I select the ID and get the related information.

Also I'm using async.waterfall() to get synchronous calls because I will make other calls that depend on the previous call.

I tried this to get the id, message and src url of attachment:

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);
}

Above will output:

[ { 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' } ]

Below is the response where I first called graph.get in the source code

and I need { data -> [message, id, attachments -> data[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_..." 
  }
}



via Classicpal

No comments:

Post a Comment