从工厂返回$ http Promise时未定义

用户名

无论我做什么,我总是从工厂API调用中获取$$stateundefined返回。我已经尝试过Promise,只是response.data从中返回.then但我没有尝试过。

我能得到正确的响应数据到我的控制器,但后来当我试图把它分配给什么,我只是得到undefined或者$$state,取决于哪种方法我使用。

我的工厂:

factory('forecastFactory', function ($http, $q, SundialConfig) {
    var Forecast = {}; 
    var weatherKey = SundialConfig.openWeatherKey;

    Forecast.dayCnt = 1; 
    Forecast.prepareCity = function (city) {
        city === undefined ? city = 'Chicago, IL' : city = city;
        return city; 
    }

    Forecast.getForecast = function (city) {
        var preparedCity = Forecast.prepareCity(city);
        var deferred = $q.defer();

        $http.jsonp('http://api.openweathermap.org/data/2.5/forecast/daily?', {
            params: {
                appid: weatherKey,
                q: preparedCity,
                cnt: Forecast.dayCnt,   
                callback: 'JSON_CALLBACK'
            }
        })
        .then(function (res) {
            console.log("success");
            deferred.resolve(res);
        })
        .catch(function (err) {
            console.log('error');
        });

        return deferred.promise; 
    }

    return Forecast;
}); 

我的控制器:

controller('ForecastController', function ($scope, $location, forecastFactory, locationService) { 
    vm = this; 
    forecastFactory.getForecast('Chicago, IL').then(function (res) {
        console.log(res);
        vm.forecast = res; 
    });
});
佐伊卜·伊亚兹(Zohaib Ijaz)

我认为您不需要使用,$q因为$ http返回了promise,

你可以做

Forecast.getForecast = function(city) {
        var preparedCity = Forecast.prepareCity(city);
        return $http.jsonp('http://api.openweathermap.org/data/2.5/forecast/daily?', {
            params: {
                appid: weatherKey,
                q: preparedCity,
                cnt: Forecast.dayCnt,   
                callback: 'JSON_CALLBACK'
            }
        })
        .then(function(res) {
      console.log("success");
      return res.data;

    })

    .catch(function(err) {
      console.log('error')
      return []; // or {} depending upon required data
    });
    }

在控制器中,执行与现在相同的操作

另一种方法是简单地返回$ http返回的promise

Forecast.getForecast = function(city) {
        var preparedCity = Forecast.prepareCity(city);

        return $http.jsonp('http://api.openweathermap.org/data/2.5/forecast/daily?', {
            params: {
                appid: weatherKey,
                q: preparedCity,
                cnt: Forecast.dayCnt,   
                callback: 'JSON_CALLBACK'
            }
        }) 
    }

并在控制器中执行此操作

Sundial.Controllers.

controller('ForecastController', ['$scope', '$location', 'forecastFactory', 'locationService', function($scope, $location, forecastFactory, locationService) { 

    vm = this; 

    forecastFactory.getForecast('Chicago, IL').then(function(res) {
        console.log(res)
        vm.forecast = res.data; 
    }, function(err){
          // do something
     })

}]); 

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

AngularJS $ http.get返回未定义且$ http()不是函数

来自分类Dev

AngularJS:返回$ http的测试工厂承诺

来自分类Dev

为什么该工厂返回未定义?

来自分类Dev

使用Promise返回未定义

来自分类Dev

处理路线时出错,工厂未定义

来自分类Dev

角度控制器/工厂返回未定义的对象

来自分类Dev

AngualrJS $ http返回未定义?

来自分类Dev

Ember RSVP Promise resolve始终返回未定义的值

来自分类Dev

角工厂保持返回未定义

来自分类Dev

设置状态但打印到控制台时,Promise返回未定义状态

来自分类Dev

为什么从“ findLocation()”返回值未定义的promise?

来自分类Dev

NodeJS Promise返回[对象未定义]

来自分类Dev

Promise在Javascript中不起作用,返回未定义

来自分类Dev

AngularJS $ http.get返回未定义且$ http()不是函数

来自分类Dev

使用Promise返回未定义

来自分类Dev

angularjs工厂未定义

来自分类Dev

AngularJS $ http.get返回未定义

来自分类Dev

解析Promise'when'返回未定义(Javascript)

来自分类Dev

HTTP可观察到的返回未定义/空

来自分类Dev

离子工厂未定义

来自分类Dev

流星:Http 调用在 Http 响应后返回未定义的响应

来自分类Dev

Promise.all 返回未定义

来自分类Dev

函数返回未定义、预期的 Promise 或价值动机

来自分类Dev

Angular 5 Promise 返回未定义

来自分类Dev

Promise mysql返回未定义,如何让函数等待结果

来自分类Dev

Promise.resolve 返回未定义

来自分类Dev

从 promise 返回的值总是未定义的

来自分类Dev

对 json 文件的 Angular http 请求返回未定义

来自分类Dev

离子原生 HTTP 返回未定义