简体   繁体   中英

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.

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

.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('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

.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:

.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. 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). There might be other things wrong with this code, but this is the reason for the error message.

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