angular-ui / ui-calendar

A complete AngularJS directive for the Arshaw FullCalendar.
http://angular-ui.github.io/ui-calendar/
MIT License
1.49k stars 729 forks source link

Reduce is not a function #448

Closed CJYate closed 7 years ago

CJYate commented 7 years ago

I'm getting console log errors on loading the page my calendar is on:

TypeError: ((intermediate value)(intermediate value)(intermediate value) || []).reduce is not a function occurring in changeWatcher:

Source is:

   this.changeWatcher = function (arraySource, tokenFn) {
                var self;

                var getTokens = function () {
---->                    return ((angular.isFunction(arraySource) ? arraySource() : arraySource) || []).reduce(
                        function (rslt, el) {
                            var token = tokenFn(el);
                            map[token] = el;
                            rslt.push(token);
                            return rslt;
                        },
                        []
                    );
                };

Also TypeError: (sources || []).reduce is not a function here:

  // @return {Array} all events from all sources
            this.allEvents = function () {
                return Array.prototype.concat.apply(
                    [],
---->               (sources || []).reduce(
                        function (previous, source) {
                            if (angular.isArray(source)) {
                                previous.push(source);
                            } else if (angular.isObject(source) && angular.isArray(source.events)) {
                                var extEvent = Object.keys(source).filter(
                                    function (key) {
                                        return (key !== '_id' && key !== 'events');
                                    }
                                );

                                source.events.forEach(
                                    function (event) {
                                        angular.extend(event, extEvent);
                                    }
                                );

                                previous.push(source.events);
                            }
                            return previous;
                        },
                        []
                    )
                );
            };

Any ideas? What info can I provide to help diagnose this?

Angular 1.5.8, on Rails 5.0.0.1.

CJYate commented 7 years ago

Fixed: This is due to misconfiguration of my controller. WAS:

      vm.eventSources = {
        events: [{
          title: "Do some development",
          start: '2016-08-12',
          end: '2016-10-22'
        }, {
          title: "Wave",
          start: '2016-09-09',
          end: '2016-09-10'
        }],
        color: 'green',
        textColor: 'yellow'
      };

Should be:

  vm.eventSources = [{
        events: [{
          title: "Do some development",
          start: '2016-08-12',
          end: '2016-10-22'

        }, {
          title: "Wave",
          start: '2016-09-09',
          end: '2016-09-10'
        }],
        color: 'green',
        textColor: 'yellow'
      }];