0

This is, I believe, what is causing the Issue in this pervious Question. I have looked here and here but I still am not understanding what is happening. When I log to the console the value of temp I get the result 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31 as expected but when I use var month = $q.defer() and month.resolve(temp) and try to log the value of month.promise I get [Object object] instead of the same as temp.

Is that what should be Expected?

if No - What am I doing incorrect ( This is why I I'm using $q.defer() in case its a factor)

if Yes - How Can I convert month.promise to a more consumable form? i.e. if temp is an array how to I access month.promise as an array where temp[0] = month.promise[0]

below is my code example but all of my controller & service can be found here, I basically want to just return an array (or JSON) to my controller instead of [Object object]

var month = $q.defer();
$http.get('getMonth.php?date=' + date)
    .success(function (data, status, headers, config) {

    var temp = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31];
    console.log("Temp : " + temp + " !!!");
    month.resolve(temp);

    //month.resolve(data);
    //console.log("data "+data[0]);
    console.log("resolved " + month);
    console.log("resolved[0] " + month[0]);
    console.log("resolved.promise " + month.promise + " !!!");
    console.log("resolved.promise[0] " + month.promise[0]);

});

UPDATE

on the advice of @CuongLe below I have added the below code and I can successfully log my data but I still get [Object object] when I return it

var promise = month.promise;
promise.then(
    function (data) {
        console.log(data);
});
Community
  • 1
  • 1
jonnie
  • 12,260
  • 16
  • 54
  • 91

2 Answers2

1

From document:

The purpose of the deferred object is to expose the associated Promise instance as well as APIs that can be used for signaling the successful or unsuccessful completion of the task.

So, you should continue to call then with success method to get temp

var promise = month.promise;
promise.then(
    function(temp){
    console.log(temp);
});

Your Service:

angular.module('testApp', [])
    .factory('memberFactory', function ($http, $q) {

    var getMonth = function (date) {
        var month = $q.defer();

        $http.get('getMonth.php?date=' + date)
            .success(function (data, status, headers, config) {

            var temp = [1, 2, 3, 4, 5, 6, 7,...];
            month.resolve(temp);
        });           
        return month.promise;
    }

    return {
        getMonth: getMonth
    };  
});

The controller:

function myController($scope, memberFactory) {

    promise = memberFactory.getMonth("2013-08-01 06:30:00");

     promise.then(
        function (monthDays) {
           console.log("monthDays : " + monthDays + " !!!");

            var dates = [];
            for (var i = 0; i < monthDays.length; i++) {
                if (i % 7 == 0) dates.push([]);
                dates[dates.length - 1].push(monthDays[i]);
            }

            $scope.dates = dates;
        });

}
cuongle
  • 74,024
  • 28
  • 151
  • 206
  • @CurongLe I'm sorry I don't understand, I tried this an I just get `TypeError: Object # has no method 'then'` – jonnie Aug 09 '13 at 10:02
  • @CurongLe that works to print out on the console but how do I return it, as I still just get `[Object oject]` when I do? I have added the full controller and service code [here](http://gw.gd/O5k2) is that helps – jonnie Aug 09 '13 at 10:19
  • @CurongLe You are a Legend thank you, it make far more sense now – jonnie Aug 09 '13 at 10:46
1

Use then method which returns a promise:

obj.getMonth = function (date) {
    var month = $q.defer();
    $http.get('getMonth.php?date=' + date)
        .then(function (data, status, headers, config) { 
            var temp = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 
                16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31]; 
            month.resolve(temp); 
        });
    return month.promise;         
}
AlwaysALearner
  • 43,759
  • 9
  • 96
  • 78
  • That allows me to print the entire array using `response =memberFactory.getMonth(date); $scope.month = response` but I still cannot access indiviudal item lik this `$scope.day = response[0]` – jonnie Aug 09 '13 at 10:43