0

So, similar to my my previous question here (I was unsure whether to make a new question or edit the old one), I managed to sucessfully parse the body JSON response using the code seen below.

However when attempting to do the same to the event_calendar_date, I get this: Failed with: TypeError: Cannot read property 'und' of undefined. I would have assumed that to get to the next object in the array the method would be the same for both, however that does not appear to be the case.

JSON response from the server:

[

    {
        "revision_uid":"1",
        "body":{
            "und":[
                {
                    "value":"Blah Blah.",
                    "summary":"",
                    "format":null,
                    "safe_value":"Blah Blah.",
                    "safe_summary":""
                }
            ]
        },
        "event_calendar_date":{
            "und":[
                {
                    "value":"2015-07-20 14:00:00",
                    "value2":"2015-07-20 20:00:00",
                    "timezone":"America/New_York",
                    "timezone_db":"America/New_York",
                    "date_type":"datetime"
                }
            ]
        }
    }

]

My Code:

Parse.Cloud.httpRequest({
    method: "GET",
    url: 'http://www.scolago.com/001/articles/views/articles',
    success: function(httpResponse) {
        var data = JSON.parse(httpResponse.text);
        var articles = new Array();
        for (var i = 0; i < data.length; i++) {
            var Articles = Parse.Object.extend("Articles"),
                article = new Articles(),
                content = data[i];

            article.set("body", content.body.und[0].value);
            article.set("vid", content.vid);
            article.set("title", content.title);

            article.set("events", content.event_calendar_date.und[0].value);

            articles.push(article);
        };


        // function save(articles);
        Parse.Object.saveAll(articles, {
            success: function(objs) {
                promise.resolve();
            },
            error: function(error) {
                console.log(error);
                promise.reject(error.message);
            }
        });

    },
    error: function(error) {
        console.log(error);
        promise.reject(error.message);
    }
});
Community
  • 1
  • 1
uknj
  • 89
  • 1
  • 13
  • 1
    Do a console.log on var data, that will show you in the inspector the structure of the data then you can see what it has. This will help you debug the issue – mic Sep 05 '15 at 08:11

1 Answers1

2

I am afraid you didn't give us all code, or you are using not the code you posted in this question, because it should run fine, as you can see in this demo:

for (var i = 0; i < data.length; i++) {
    var content = data[i];
    console.log(content.event_calendar_date.und[0].value);
};

EDIT and solution

As I expected, JSON you are getting doesn't have field event_calendar_date for 2nd, and 3rd object. I checked in your full code on GitHub.

First object has correct event_calendar_date field, but next records don't have. See how is JSON you are getting formatted - http://www.jsoneditoronline.org/?id=b46878adcffe92f53f689978873a3474.

You need to check first if content.event_calendar_date is present in record in your current loop iteration or add event_calendar_date to ALL records in JSON response.

Daniel Kmak
  • 18,164
  • 7
  • 66
  • 89
  • Well [here](https://github.com/Scolago/CloudCode/blob/master/cloud/main.js) is the full code if you think it will help. But afaik, I posted all relevant code. – uknj Sep 05 '15 at 09:46
  • I dont believe I will be able to change the JSON response, so would a simple if statement to check for `content.event_calendar_date` be suitable for the task? Thanks for the help. – uknj Sep 05 '15 at 10:02
  • 1
    Yes. Check http://stackoverflow.com/questions/455338/how-do-i-check-if-an-object-has-a-key-in-javascript. – Daniel Kmak Sep 05 '15 at 10:04