0

I'm returning a JSON object using a 3rd party API. When I print out the variable I get the following:

category: 
{
    "sys":
    {
        "id":"44uyBvGjFmsmoU6uqiiSOq",
        "revision":1,
        "type":"Entry",
        "locale":"en-US",
        "contentType":
        {
            "sys":
            {
                "id":"2V5tBa4wf6cuMSIu4McuUc",
                "type":"Link",
                "linkType":"ContentType"
            }
        },
        "createdAt":"2014-09-13T22:40:39.901Z",
        "updatedAt":"2014-09-13T22:40:39.901Z",
        "space":
        {
            "sys":
            {
                "id":"t667fybp3v46",
                "type":"Link",
                "linkType":"Space"
            }
        }
    },
    "fields":
    {
        "name":"birth control"
    }
}

But when I try and access the variables using the following, I keep on getting "Possibly unhandled TypeError: Cannot read property 'name' of undefined" :

if (o.fields.category)
{
    var str = JSON.stringify(o.fields.category, undefined, 2);
    console.log(str);

    var json_category = JSON.parse(str);
    // console.log("--------------Category for the resource: " + o.fields.category);
    $.each(json_category, function(index, object){
        alert(10);
        var category = {};
        // console.log("--------------Category for the resource: " + object.sys);
        category.id = object.sys.id;
        // console.log("--------------Category for the resource: " + object.fields.name);
        category.name = object.fields.name;
        // console.log("--------------Category for the resource: " + object.sys.updatedAt);
        category.updatedAt = object.sys.updatedAt;
        categories.push(category);
    });
 }

Any help is greatly appreciated.

zee
  • 656
  • 2
  • 7
  • 30
  • What does your call to this API look like? The problem is very likely to be that your API is asynchronous and you're not properly waiting for the result. – Pointy Sep 14 '14 at 12:50
  • I'm using Contentful and their Javascript library https://www.contentful.com/developers/documentation/content-delivery-api/ – zee Sep 14 '14 at 12:55
  • Oh I see what's happening. – Pointy Sep 14 '14 at 12:58
  • 1
    Well it's hard to say; it would be much more useful to debug from *inside* your callback to the `$.each()` loop. You don't have to stringify the object to log it; use `console.dir(object);` and you'll be able to inspect the object in the developer console. – Pointy Sep 14 '14 at 13:01
  • I think I see what you mean. Do you think you can provide a bit of code. Sorry not the strongest with JSON. – zee Sep 14 '14 at 13:01
  • 1
    Just replace that `alert(10)` call with `console.log("index: " + index + " object: ", object);` and have the browser developer console open. (The "Console" tab; the developer console has a lot of stuff in it nowadays.) – Pointy Sep 14 '14 at 13:04
  • 1
    You can not access id directly use 'category.sys.id' instead of 'category.id', do the same for name. – Aria Sep 14 '14 at 13:37
  • Thanks guys, I've added the code that I used below – zee Sep 15 '14 at 10:48

1 Answers1

0

Changed my code to the following. Thanks to Pointy for providing the assistance, much appreciated.

if (o.fields.category)
{
    console.log(o.fields.category.fields.name);

    var category = {};
    category.id = o.fields.category.sys.id;
    category.name = o.fields.category.fields.name;
    category.updatedAt = o.fields.category.sys.updatedAt;
    categories.push(category);
}

record.categories = categories;
record.searchTags = search_tags;
records.push(record);
zee
  • 656
  • 2
  • 7
  • 30