1

Let me know if you need more information to solve this issue.

I'm trying to retrieve local data using a factory and calling it from a controller.

Controller.js:

storage.getAllLocalInfo().then(function(data){
    console.log(data.distractions[0].type);
    // produces 'url'
            // console.log(data.distractions);
    // produces the following
    //  0: Object
    //      oldTxt: "youtube.com"
    //      txt: "youtube.com"
    //      type: undefined
    $scope.distractions = data.distractions;
            // This only happens when executing the line above.
    // Without that line, there is no inconsistency.
});

How is it that if I ask for the nested property (type), the console returns the correct value, but when I ask for the entire object, type returns as undefined. This only happens when I include the line with $scope.distractions. And the associated factory:

var getAllLocalInfo = function() {
    var deferred = $q.defer();
    chromeStorage.get( null , function( data ) {
        if (!data) {
            deferred.reject();
        } else {
            deferred.resolve(data);
        }
    });
    return deferred.promise;
};

Can anyone explain the strange behavior of console.log in the controller? I'm also new to promises so that might be what I'm messing up, though this also behaved the same when using a callback in the factory instead of a promise.

willthefirst
  • 1,499
  • 2
  • 15
  • 21
  • Sorry, I don't get it: What do you mean by "only happens when executing the line above" ? Could you explain the "weirdness" a little more _verbosely_ ? – gkalpak Jan 04 '14 at 07:56
  • updated: How is it that if I ask for the nested property (`type`), the console returns the correct value, but when I ask for the entire object, `type` returns as `undefined`. This only happens when I include the line with `$scope.distractions`. – willthefirst Jan 04 '14 at 19:51
  • Could you post the whole extension code somewhere, so I can reproduce the problem ? – gkalpak Jan 04 '14 at 19:59
  • Here you go: https://github.com/willthefirst/decide/tree/master/extension – willthefirst Jan 05 '14 at 02:24
  • I played arround, but I could not reproduce the problem (e.g. `periodBeingUsed` was consistently `false` in both `console.log`'s). If possible add some steps/instructiosn to reproduce the problem and/or post a **[SSCCE](http://sscce.org)**. – gkalpak Jan 05 '14 at 09:01

0 Answers0