简体   繁体   中英

Resolver function doesn't return anything

In my resolver function I have loop that call http service in each itaration:

function detailedReportDataResolver($stateParams, reportService) {
    var promises = [];
    var frequenciesArr = $stateParams.frequencies.split(',').map(function (item) { return parseInt(item, 10); });

     angular.forEach(frequenciesArr, function (freq, key) {
         reportService.getDetailsReport($stateParams.clientId, $stateParams.date, freq).then(function (result) {
             promises.push(result.data);
        });
     });
     return $q.all(promises);         
}

Here how I call the resolver function in my module:

  resolve: {
    detailedReportData: ["$stateParams", "reportService", detailedReportDataResolver]
      }

Here is service:

(function () {
    "use strict";

    angular.module("reportBuilder").factory("reportService", ["$http", "config", reportService]);

    function reportService($http, config) {
        var serviceUrl = config.baseUrl + "api/Reports/";
        var service = {
            getDetailsReport: detailsReport
        };

        return service;


        function detailsReport(clientId, date, frequencies) {

            if (!date && !clientId) return null;
            return $http.get(serviceUrl + "ReportDetailed/" + clientId + "/" + date + "/" + frequencies);
        }

    }
})();

I need to wait for http service to finish before starting next iteration of loop and the return the result. For this purpose I use $q.all in resolver finction above.

But resolver function doesn't return anything.Any idea what I do wrong?Why resolver function doesn't returns anything?

Array of promise is not composed correctly. Can you try:

function detailedReportDataResolver($stateParams, reportService) {
    var promises = [];
    var frequenciesArr = $stateParams.frequencies.split(',').map(function (item) { return parseInt(item, 10); });

     angular.forEach(frequenciesArr, function (freq, key) {
         var promise = reportService.getDetailsReport($stateParams.clientId, $stateParams.date, freq).then(function (result) {
             return result.data;
        });
        promises.push(promise);
     });
     return $q.all(promises);         
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM