Cannot set property "XY" to undefined

I have the following code:

var favourites = JSON.parse(localStorage.getItem("favourites"));

Service.all().then(function (multiple) {
        var array = [];            

        for (var i = 0; i < multiple.length; i++) {
           for (var j = 0; j < favourites.length; j++) {
              if (favourites[j].checked === true) {
                 if (multiple[i].Name === favourites[j].name) {

                    Service.getAllBySomething(multiple[i].Id).then(function (resources) {
                       var arrayOfSomething = [];

                       for (var k = 0; k < resources.length; k++) {
                                                     arrayOfSomething.push(resources[k].ResourceCategoryId);
                       }                           
                       arrayOfSomething = arrayOfSomething .filter(function (elem, pos, arr) {
                          return arr.indexOf(elem) == pos;
                       });
                       multiple[i].existingProperty= arrayOfSomething;
                    });                        
                    array.push(multiple[i]);
                 }
              }                  
           }
        }            
        $scope.vendors = array;
     });

      

My problem is that it says every time "Can't set existingProperty to undefined". And I don't know why multiple [i] should be undefined on this line:

multiple[i].existingProperty= arrayOfSomething;

      

The property exists, I'm sure. And it is defined, it is an empty array. And this empty array that I want to replace with my array in a loop. Where is the mistake? How can I fill the existingProperty with my array?

+3


source to share


1 answer


is Service.getAllBySomething

asynchronous in any way? Because in this case, by the time the callback function was executed, i

(only in the closure) it was moved to the end of the array.

Use optional closure to freeze the value i

while sending the asynchronous call, for example:



Service.getAllBySomething(multiple[i].Id).then(function(i){
     return function (resources) {
                   var arrayOfSomething = [];

                   for (var k = 0; k < resources.length; k++) {
                                                 arrayOfSomething.push(resources[k].ResourceCategoryId);
                   }                           
                   arrayOfSomething = arrayOfSomething .filter(function (elem, pos, arr) {
                      return arr.indexOf(elem) == pos;
                   });
                   multiple[i].existingProperty= arrayOfSomething;
                };
}() );    

      

Notice the new function that i

takes as a parameter and returns the previously used function. I call immediately ( IIFE ) to return a function to pass as a callback. Inside this function, the value of i will be the same as when this asynchronous call was sent, as it was copied when used as a parameter.

+1


source







All Articles