Home > Enterprise >  What is the reason my code cannot be parsed
What is the reason my code cannot be parsed

Time:01-02

I am trying to parse data. But this error comes:

Error parsing response TypeError: Cannot read properties of undefined (reading 'response')

var result:

{
  multistatus: {
    '$': { xmlns: 'DAV:', 'xmlns:C': 'urn:ietf:params:xml:ns:caldav' },
    response: [ [Object], [Object] ]
  }
}

Code:

var data = result['D:multistatus']['D:response'];

          if(data) {
              data.map((event) => {
                var ics = event['D:propstat'][0]['D:prop'][0]['C:calendar-data'][0];
                var jcalData = ICAL.parse(ics);
                var vcalendar = new ical.Component(jcalData);
                var vevent = vcalendar.getFirstSubcomponent('vevent');
                reslist.push(vevent)
              });
            }

Error:

Error parsing response TypeError: Cannot read properties of undefined (reading 'response')

(Error to the line var data ...)

i expected to parse the data on correctly. Afterwards the events get filtered again.

CodePudding user response:

You data structure is this:

{
  multistatus: {
    '$': { xmlns: 'DAV:', 'xmlns:C': 'urn:ietf:params:xml:ns:caldav' },
    response: [ [Object], [Object] ]
  }
}

Which means you should use the actual property names multistatus and response and change this:

var data = result['D:multistatus']['D:response'];

to this:

var data = result['multistatus']['response'];

I don't know why you put the D: at the front of the property names. Those are not present in the actual property names you show and must be removed.

Also, you should not be using var any more. Use either let or const.


The error message you get:

 Cannot read properties of undefined (reading 'response')

Comes because this first part:

var data = result['D:multistatus']

results in undefined (since the D:multistatus property does not exist) and then you try to reference the second property off undefined, with this:

var data = result['D:multistatus']['D:response'];

you get that error as undefined is not an object and thus gives you an error when you try to reference a property on it.

  • Related