0

I'm working with Fullcalendar and I'm trying to get resources as function

resources: function(callback){
          var manageEvent = new ManageEvent();
          var request = manageEvent.getEmployees();

          request.always(function (param) {
            //location.reload();
            var list = [];
            var emp;
            for (var elem in param) {
              emp = param[elem];
              list.push({
                  'id': emp['cp_collaboratore'],
                  'title': emp['cognome_col']
              });
            }

            var t = JSON.stringify(list);

            callback(t);

          });
          request.catch(function (param) {
              alert('errore');
          });
        },

I checked the variable 't' through log and it shows the following result:

[{"id":"1","title":"name_1"},{"id":"2","title":"name_2"},{"id":"3","title":"name_3"},{"id":"5","title":"name_4"},{"id":"9","title":"name_5"}]

but it don't works and shows the following error message:

Uncaught TypeError: resourceInputs.map is not a function
at ResourceManager.setResources
Cœur
  • 37,241
  • 25
  • 195
  • 267
Marco
  • 49
  • 3
  • It's an array-like object, not array. https://stackoverflow.com/questions/29707568/javascript-difference-between-array-and-array-like-object – Laurynas Aug 05 '18 at 17:39
  • @LaurynasGerbutavicius that's not the issue. See my answer. The OP is passing a string, not any kind of array. If their variable were not stringified, it would be an array already. – ADyson Aug 07 '18 at 18:09

1 Answers1

-1

You just need to write

callback(list);

t in your code is a string, because you converted your list array into a string using JSON.stringify(). But fullCalendar expects an actual array, not a string. It can't run functions or read individual properties from a string.

You can remove the line var t = JSON.stringify(list); completely, it's not needed.

Generally the only reason you'd use stringify() is if you wanted to log the value to your console for debugging, or convert the array into JSON if you wanted to send it somewhere else using AJAX. It makes no sense to pass arrays and objects around inside JavaScript as serialised strings, when you can just use the objects themselves.

ADyson
  • 57,178
  • 14
  • 51
  • 63
  • 1
    Thank you so much!! – Marco Aug 07 '18 at 16:55
  • @Marco no problem. The best way you can thank me though is to mark the answer as "accepted". Click the tick symbol next to the question so it turns green. Thankyou. – ADyson Aug 07 '18 at 18:08