0

I want to communicate from backend with the calendar using caldav of zoho mail using nodejs. Could anyone suggest me how to implement it? I am using plugin node-caldav-mod

I tried this piece of code which doesn't seem to be working.

var caldav = require("node-caldav-mod");
var moment = require('moment-timezone');
var express = require('express');
var app = express();
var xmljs = require("libxmljs");
var https = require("https");

var CalendarId = "2123123123";
var url = "https://calendar.zoho.com/caldav/{CalendarId}/events";
var username = "username"
var password = "password"
var timeFormat = "YYYYMMDDTHHmms";
var getTodayEvent = function (callback){
    var startDate = moment().set({'hour': 0,'minute': 0,'second': 0}).format(timeFormat) + "Z";
    var endDate = moment().set({'hour': 23,'minute': 59,'second': 59}).format(timeFormat) + "Z";
    var output = {};
    output.startDate = startDate;
    output.endDate = endDate;

    caldav.getEvents(url, username, password, startDate, endDate, function(response){
        console.log(response);
     callback(response);
    });
}
var findPropertyNameByRegex = function(o, r) {
    var key;
    for (key in o) {
      if (key.match(r)) {
        return key;
      }
    }
    return undefined;
  };
  function compare(a,b) {

    var startDate_a = findPropertyNameByRegex(a, "DTSTART");
    var startDate_b = findPropertyNameByRegex(b, "DTSTART");

    if (a[startDate_a] < b[startDate_b])
      return -1;
    else if (a[startDate_a] > b[startDate_b])
      return 1;
    else
      return 0;
  }


    app.get('/today',function(req, res){
        getTodayEvent(function(events){
            events.sort(compare);
            res.send("Communication set up properly!")
        })
    });

This is the error which I am getting

  Parsing.....
undefined
Error parsing response
TypeError: Cannot read property 'D:multistatus' of undefined

Could somebody tell me what's wrong with this code?

Lovika
  • 577
  • 2
  • 10
  • 21
  • Can anybody suggest me a solution to this problem? I have been breaking my head over this since last 2 days. Any help would be much appreciated – Lovika Oct 16 '17 at 09:19

1 Answers1

2

Could somebody tell me what's wrong with this code?

Yes. Your error seems to come from this line in the module you seem to use:

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

Which is utter non-sense. D: is just a namespace prefix and can be anything the server chooses. A CalDAV client needs to properly parse and process XML namespaces.

Solution: Use a proper module, or just write it on your own.

hnh
  • 13,957
  • 6
  • 30
  • 40