0

I am trying to pass a JSON to another function in my Javascript code. But I am struggling to do so. I have included the simplified version of the code below.

function createJson() {
    var det = {};
    FB.api(
        '/me',
        'GET', {
            "fields": "id, photos{id,name}"
        },
        function(response) {
            console.log(response);
            for (var i = 0; i < response.photos.data.length; i++) {
                var obj1 = {
                    id: response.photos.data[i].id,
                    name: response.photos.data[i].name
                };
                det[i] = obj1;
            }
        }
    );
    getLikesJson(det);

From what I understand, I should now be able to work with 'det' within the function 'getLikesJson', instead I am getting [object Object]. Am I doing something wrong?

AtheistP3ace
  • 9,611
  • 12
  • 43
  • 43
  • 1
    Use `JSON.stringify(det)` to use JSON instead of JS objects. Then you'll see `{}` instead of `[object Object]`. – Bergi Mar 08 '16 at 17:06
  • Thank you! Should I still be able to access the JSON details in the same way in the second function? Or do I need to do something to it in the other function? – Hannah Lloyd Mar 08 '16 at 17:26
  • No, you should not pass JSON between functions, you should pass objects. Use `JSON.stringify` only for outputs (like `alert`). Without objects, you won't be able to access properties. And see the duplicate on why you're currently not having any properties in the object at all. – Bergi Mar 08 '16 at 17:29

0 Answers0