简体   繁体   English

试图做出承诺的决心

[英]trying to do a resolve with a promise

I am trying to do a resolve with a promise due to an issue with a filters I am working on, but, my resolve isn't working yet. 由于我正在使用的过滤器存在问题 ,我正在尝试做出promiseresolve ,但是我的解决方案仍无法使用。

I am doing this because in a question I did before some one asked me to do a resolve which is the must logic solution I got from other people. 我之所以这样做,是因为在一个问题上,有人问我要做一个解决方案,这是我从其他人那里得到的必不可少的逻辑解决方案。

This what the console returns: 控制台返回以下内容:

localhost:1337/lines/sports/undefined:1
GET http://localhost:1337/lines/sports/undefined 400 (Bad Request)

Take a look at my code: 看一下我的代码:

app.js app.js

.state('app.sports', {
  url:'/sports',
  views:{
    menuContent:{
      templateUrl:'templates/sportsList.html',
      controller:'SportsController',
      resolve: {
        Sports: function(SportsFactory, AuthFactory, $q) {
          var defer = $q.defer();
          console.log(AuthFactory);
          AuthFactory.getCustomer().then(function() {
            SportsFactory.getSports().then(function(sports) {
              defer.resolve(sports);
            });
          });
          return defer.promise;
        }
      }

controller.js controller.js

.controller('SportsController', function($scope, $state,
             AuthFactory, SportsFactory, Sports) {
    $scope.query = '';
    $scope.sports = Sports;
    $scope.sports = [];
    $scope.customer = {};
   });

    AuthFactory.getCustomer().then(function(customer) {
      $scope.customer = customer;
      SportsFactory.getSportsWithLeagues(customer).then(function(sports) {           
        if (sports.length) {
          $scope.sports = sports;
        }else {
          AuthFactory.logout();
        }
      }, function(err) {
        console.log(err);
      });
    }, function(err) {
      console.log(err);
    });

service.js service.js

.factory('SportsFactory', function($http, $q, AuthFactory,
          LocalForageFactory, LeaguesFactory, CONSTANT_VARS) {
    return {
      getSports: function(agent) {
        var defer = $q.defer();

        LocalForageFactory.retrieve(CONSTANT_VARS.LOCALFORAGE_SPORTS)
          .then(function(sports) {
            if (!_.isNull(sports)) {
              defer.resolve(_.values(sports));
            }else {
              $http.get(CONSTANT_VARS.BACKEND_URL + '/lines/sports/' + agent)
                .success(function(sports) {
                  //forcing array instead of object
                  sports = _.values(sports);
                  sports = _.sortBy(sports, function(sport) {
                    return sport.priority;
                  });
                  LocalForageFactory.set(CONSTANT_VARS.LOCALFORAGE_SPORTS, sports);
                  defer.resolve(sports);
                })
                .error(function(err) {
                  defer.reject(err);
                });
            }
          });
        return defer.promise;
      },
      getSportsWithLeagues: function(customer) {
        var _this = this,
          defer = $q.defer(),
          rejection = function(err) {
            defer.reject(err);
          },
          sportsLength;

        LocalForageFactory.retrieve(CONSTANT_VARS.LOCALFORAGE_SPORTS_LEAGUES)
          .then(function(sportLeagues) {
            if (!_.isNull(sportLeagues)) {
              //forcing array instead of object

              sportLeagues = _.values(sportLeagues);
              defer.resolve(sportLeagues);
            }else {
              _this.getSports(customer.agent).then(function(sports) {
                sportsLength = sports.length;
                LeaguesFactory.getLeagues({
                  sportIds: _.pluck(sports, 'id'),
                  lineProfile: customer.lineProfile,
                  betLimit: customer.betLimit
                }).then(function(leagues) {
                  _.each(sports, function(sport) {
                    sport.leagues = _.filter(leagues, function(league) {
                      return sport.id === league.sport.id;
                    });
                  });
                  //forcing array instead of object
                  sports = _.values(sports);
                  LocalForageFactory.set(CONSTANT_VARS.LOCALFORAGE_SPORTS_LEAGUES, sports);
                  defer.resolve(sports);
                }, rejection);
              }, rejection);
            }
          }, rejection);
        return defer.promise;
      }
    };
  });

and this is the authFactory: 这是authFactory:

.factory('AuthFactory', function($q, $http, $state,
          LocalForageFactory, CONSTANT_VARS) {
    return {
      /**
       * This function logs the customer, if the customer exists,
       * Customer data is saved in order to perform actions,
       * if not, an error message is returned.
       * @param credentials a json with this format {username: 'jhon', password:'D03'}
       * @returns {Animation.promise|promise}
       */
      login: function(credentials) {
        var defer = $q.defer(),
          _this = this;

        $http.post(CONSTANT_VARS.BACKEND_URL + '/auth/login',
          credentials
        ).success(function(data) {
            if (data.error) {
              defer.reject(data);
            }
            _this.setCustomer(data).then(function(customer) {
              defer.resolve(customer);
            }, function(err) {
              defer.reject(err);
            });
          }).error(function(data, status) {
            if (status === 0) {
              data = new Error('Backend is down');
              data.raw = {};
            }
            defer.reject(data);
          });
        return defer.promise;
      },
      setCustomer: function(customer) {
        var defer = $q.defer();
        LocalForageFactory.set(CONSTANT_VARS.LOCALFORAGE_CUSTOMER, customer).then(function(customer) {
          /*Removing LocalForage Items*/
          LocalForageFactory.remove(CONSTANT_VARS.LOCALFORAGE_LEAGUES);
          LocalForageFactory.remove(CONSTANT_VARS.LOCALFORAGE_SPORTS_LEAGUES);
          LocalForageFactory.remove(CONSTANT_VARS.LOCALFORAGE_SPORTS);
          defer.resolve(customer);
        }, function(err) {
          $state.go('app.login');
          defer.reject(err);
        });
        return defer.promise;
      },
      updateCustomer: function(customer) {
        var defer = $q.defer();
        LocalForageFactory.set(CONSTANT_VARS.LOCALFORAGE_CUSTOMER, customer).then(function(customer) {
          defer.resolve(customer);
        }, function(err) {
          $state.go('app.login');
          defer.reject(err);
        });
        return defer.promise;
      },
      getCustomer: function() {
        var defer = $q.defer();
        LocalForageFactory.retrieve(CONSTANT_VARS.LOCALFORAGE_CUSTOMER).then(function(customer) {
          if (customer) {
            defer.resolve(customer);
          }else {
            defer.reject(new Error());
          }
          defer.reject(customer);
        }, function(err) {
          defer.reject(err);
        });
        return defer.promise;
      },
      logout: function() {
        var defer = $q.defer();

        this.getCustomer().then(function(credentials) {
          $http.post(CONSTANT_VARS.BACKEND_URL + '/auth/logout',
          credentials
        ).success(function(data) {
            if (data.error) {
              defer.reject(data);
            }
            /*Removing LocalForage Items*/
            LocalForageFactory.remove(CONSTANT_VARS.LOCALFORAGE_LEAGUES);
            LocalForageFactory.remove(CONSTANT_VARS.LOCALFORAGE_SPORTS_LEAGUES);
            LocalForageFactory.remove(CONSTANT_VARS.LOCALFORAGE_SPORTS);
            LocalForageFactory.remove(CONSTANT_VARS.LOCALFORAGE_CUSTOMER);
            defer.resolve(data);
          }).error(function(data) {
            defer.reject(data);
          });
        }, function(err) {
          $state.go('app.login');
          defer.reject(err);
        });
        return defer.promise;
      },
      updateToken: function(token) {
        var _this = this,
            defer = $q.defer();
        this.getCustomer().then(function(customer) {
          customer.token = token;
          _this.updateCustomer(customer).then(function(savedCustomer) {
            defer.resolve(savedCustomer);
          }, function(err) {
            defer.reject(err);
          });
        }, function(err) {
          defer.reject(err);
        });
        return defer.promise;
      },
      customerInfo: function() {
        var defer = $q.defer();
        this.getCustomer().then(function(customer) {
          $http.post(CONSTANT_VARS.BACKEND_URL + '/auth/info', customer)
            .success(function(data) {
              defer.resolve(data);
            })
            .error(function(err) {
              defer.reject(err);
            });
        }, function(err) {
          defer.reject(err);
        });
        return defer.promise;
      }
    };
  });

In app.js your doing SportsFactory.getSports() , but getSports expects 'agent' argument. 在app.js中,您正在执行SportsFactory.getSports(),但是getSports需要'agent'参数。 Since you are not supllying it with 'agent' this: '/lines/sports/' + agent equals this: lines/sports/undefined, which is why you are getting 400 (bad request). 由于您不使用'agent'来补充它,所以:'/ lines / sports /'+ agent等于:lines / sports / undefined,这就是为什么您得到400(错误请求)的原因。 There might be other things wrong with this code, but this is the reason for the error message. 该代码可能还有其他问题,但这是错误消息的原因。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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