datebook icon indicating copy to clipboard operation
datebook copied to clipboard

TypeError: Cannot read properties of undefined (reading 'length')

Open mark05e opened this issue 2 years ago • 1 comments

Might be related to https://github.com/jshor/datebook/issues/162

I have a demo code setup

const datebook = require('datebook');
const session1 = {
    title: 'Session 1',
    location: 'online',
    description: 'Example description',
    start: new Date('2021-09-01T17:00:00'),
    end: new Date('2021-09-01T19:00:00')
  };
  const session2 = {
    title: 'Session 2',
    location: 'online',
    description: 'Example description',
    start: new Date('2021-09-08T17:00:00'),
    end: new Date('2021-09-08T19:00:00')
  };
  const session3 = {
    title: 'Session 3',
    location: 'online',
    description: 'Example description',
    start: new Date('2021-09-15T17:00:00'),
    end: new Date('2021-09-15T19:00:00')
  };

config = []
config.push(session1)
config.push(session2)
config.push(session3)
// console.log(config)

var ical = 0

config.forEach( (c,i) => {
    console.log("Index: ", i, "\n Data: ", c)
    if (i == 0) {ical = new datebook.ICalendar(c)}
    else {ical.addEvent(c)}
})
// ical.render() // 👈 I get an error on this
console.log("-----------\n ical object:\n",ical)

I get the below output when I try to log the ical object

PS D:\mytests> node .\datebook-demo.js
Index:  0
 Data:  {
  title: 'Session 1',
  location: 'online',
  description: 'Example description',
  start: 2021-09-01T21:00:00.000Z,   
  end: 2021-09-01T23:00:00.000Z      
}
Index:  1
 Data:  {
  title: 'Session 2',
  location: 'online',
  description: 'Example description',
  start: 2021-09-08T21:00:00.000Z,   
  end: 2021-09-08T23:00:00.000Z
}
Index:  2
 Data:  {
  title: 'Session 3',
  location: 'online',
  description: 'Example description',
  start: 2021-09-15T21:00:00.000Z,
  end: 2021-09-15T23:00:00.000Z
}
-----------
 ical object:
 e {
  isAllDay: false,
  description: 'Example description',
  title: 'Session 1',
  location: 'online',
  start: 2021-09-01T21:00:00.000Z,
  end: 2021-09-01T23:00:00.000Z,
  params: {},
  attendees: [],
  setText: [Function (anonymous)],
  setTimestamps: [Function (anonymous)],
  setParam: [Function (anonymous)],
  recurrence: undefined,
  additionalEvents: [
    {
      title: 'Session 2',
      location: 'online',
      description: 'Example description',
      start: 2021-09-08T21:00:00.000Z,
      end: 2021-09-08T23:00:00.000Z
    },
    {
      title: 'Session 3',
      location: 'online',
      description: 'Example description',
      start: 2021-09-15T21:00:00.000Z,
      end: 2021-09-15T23:00:00.000Z
    }
  ],
  properties: [
    'CLASS:PUBLIC',
    'DESCRIPTION:Example description',
    'LOCATION:online',
    'SUMMARY:Session 1',
    'TRANSP:TRANSPARENT',
    'DTSTART:20210901T210000Z',
    'DTEND:20210901T230000Z'
  ],
  meta: { UID: 'glr1swqpbig', DTSTAMP: '20220407', PRODID: 'datebook' },
  setInitialParams: [Function (anonymous)],
  getAttendeeParams: [Function (anonymous)],
  getAlarmDuration: [Function (anonymous)],
  setMeta: [Function (anonymous)],
  addEvent: [Function (anonymous)],
  addProperty: [Function (anonymous)],
  addAlarm: [Function (anonymous)],
  download: [Function (anonymous)],
  render: [Function (anonymous)]
}

I get the below error when I try to render or download:

TypeError: Cannot read properties of undefined (reading 'length')
    at o (D:\mytests\node_modules\datebook\dist\datebook.js:1:14941)
    at D:\mytests\node_modules\datebook\dist\datebook.js:1:17640
    at Array.reduce (<anonymous>)
    at e.r.render (D:\mytests\node_modules\datebook\dist\datebook.js:1:17609)
    at Object.<anonymous> (D:\mytests\datebook-demo.js:37:6)
    at Module._compile (node:internal/modules/cjs/loader:1103:14)
    at Object.Module._extensions..js (node:internal/modules/cjs/loader:1157:10)
    at Module.load (node:internal/modules/cjs/loader:981:32)
    at Function.Module._load (node:internal/modules/cjs/loader:822:12)
    at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:77:12)

I'm not sure what is wrong here. Any advice on what I could be doing wrong? I'm also not sure if I got the build right in the forEach loop

[email protected] node v16.14.2

mark05e avatar Apr 07 '22 01:04 mark05e

I have updated the code

//snippet

    config.forEach( (c,i) => {
        console.log("Index: ", i, "\n Data: ", c)
        if (i == 0) {ical = new datebook.ICalendar(c)}
        else {ical.addEvent(new datebook.ICalendar(c))}  // 👈 updated line
    })

//snippet

and now I get the below output.

BEGIN:VCALENDAR
VERSION:2.0
BEGIN:VEVENT
CLASS:PUBLIC
DESCRIPTION:Example description
LOCATION:online
SUMMARY:Session 2
TRANSP:TRANSPARENT
DTSTART:20210908T210000Z
DTEND:20210908T230000Z
END:VEVENT
BEGIN:VEVENT
CLASS:PUBLIC
DESCRIPTION:Example description
LOCATION:online
SUMMARY:Session 3
TRANSP:TRANSPARENT
DTSTART:20210915T210000Z
DTEND:20210915T230000Z
END:VEVENT
BEGIN:VEVENT
CLASS:PUBLIC
DESCRIPTION:Example description
LOCATION:online
SUMMARY:Session 1
TRANSP:TRANSPARENT
DTSTART:20210901T210000Z
DTEND:20210901T230000Z
END:VEVENT
END:VCALENDAR
UID:fpg1uwh92ed
DTSTAMP:20220407
PRODID:datebook

The sequence of the entries seems to be off but I'm happy I got something. 😀

mark05e avatar Apr 07 '22 02:04 mark05e

Hi @mark05e - correct, as you point out the argument of addEvent() requires an instance of ICalendar and not just the config object.

But you are correct that the sequence appears to be off, and thanks for pointing that out - that will be fixed in the next release.

jshor avatar Nov 25 '22 21:11 jshor