gsklee / ngStorage

localStorage and sessionStorage done right for AngularJS.
MIT License
2.33k stars 461 forks source link

TypeError: Illegal invocation #161

Open ghost opened 9 years ago

ghost commented 9 years ago

Hi,

Getting this:

TypeError: Illegal invocation
    at isArrayLike (angular.js:276)
    at forEach (angular.js:332)
    at copy (angular.js:930)
    at copy (angular.js:899)
    at copy (angular.js:943)
    at Object.copy (angular.js:899)
    at $get.$storage.$apply (ngStorage.js:176)
    at angular.js:17744
    at completeOutstandingRequest (angular.js:5490)
    at angular.js:5762

Error message goes away if I disable "_last$storage = angular.copy($storage);" on line 176 of ngStorage.js

Any thoughts are really appreciated!

AngularJS 1.4.5 Material Design, from master ngStorage 0.3.9

Thanks, Dennis

egilkh commented 9 years ago

Hoy,

Thanks for the report. Any chance you could provide an example of how you use ngStorage in your app?

I use it like this in an app that uses AngularJS 1.4.5, ngMaterial 0.10.1 and ngStorage 0.3.9:

app.service('Leads', ['$rootScope', '$log', '$localStorage',
  function ($rootScope, $log, $localStorage) {
    if (!$localStorage.Leads) {
      $localStorage.Leads = [];
    }
    var leads = $localStorage.Leads;

    // ... snip ...

    return leads;
  }]);
ghost commented 9 years ago

Hey,

My use case:

  1. Calling mdDialog to create a modal
  2. Injecting $localStorage into the modal's controller to load previously stored vars
 angular
        .module('app')
        .controller('BlogImageDialogController', BlogImageDialogController);

    BlogImageDialogController.$inject = ['$scope', '$mdDialog', ..... '$localStorage'];
    function BlogImageDialogController($scope, $mdDialog, ...... localStorage) {
...

That error was appearing just because of using localStorage as DI (weird). Even after removing all the code from the modal controller I was still getting this error.

( I've found a work-around for my case though: i'm passing vars using locals: {} obj in the mdDialog config, thus avoiding $localStorage in the modal controller at all. )

Hope this helps! D.

rkabrahpudi commented 7 years ago

Hi.. After a little research i found the cause of the issue. but fixing it is upto dev's.

So you are using this to store files in the local storage. so after selecting a file, the file is injected into the localstorage but in a different way rather than as a file because angular.copy cannot copy files and in line number 178 we have

temp$storage = angular.copy(_last$storage);

This copies the file to the temp$storage incorrectly making the angular.equals throw error on the next digest cycle. So this is an issue with angular.copy not supporting deep copy of files.

Hope this helped.

DanielRuf commented 5 years ago

Hi, any update or workarounds? Can we catch this exception and ignore it?