0

No matter what I do I cannot get my redirect to work from my factory. I also tried in the controller but keeps throwing error. TypeError: Cannot read property 'model.Email' of undefined

I know the post is successful I can see the 200 and it updates perfectly. Here is what I have:

Factory function to show what I have injected:

app.factory('accountFactory', function ($http, $q, $cookieStore, $location)

My Factory:

factory.register = function (data) {
    return $http.post(urlBase + '/Register', data).then(function(response) {
        if (typeof response.data === "object") {
            $location.path('/login');
            return response.data;
        } else {
            return $q.reject(response.data);
        }
    }, function(response) {
        return $q.reject(response.data);
    });
};

Controller:

app.controller('accountCtrl', function ($scope, accountFactory) {
    $scope.hello = 'hello';

    $scope.registerUser = function () {
        var data = {
            "Email": $scope.email,
            "Password": $scope.password,
            "ConfirmPassword": $scope.confirmpass
        };

        accountFactory.register(data).then(function(data) {
            $scope.message = 'Successfully Created User!';

        }, function (error) {
            $scope.message = error.Message;

            // Invalid Email
            if (error['ModelState']['model.Email'])
                $scope.errorEmail = error['ModelState']['model.Email'][0];
            else
                $scope.errorEmail = null;

            // Invalid Pass / Confirm
            if (error['ModelState']['model.Password'])
                $scope.errorPassword = error['ModelState']['model.Password'][0];
            else if (error['ModelState']['model.ConfirmPassword'])
                $scope.errorPassword = error['ModelState']['model.ConfirmPassword'][0];
            else if (error['ModelState'][''])
                $scope.errorPassword = error['ModelState'][''][0];
            else
                $scope.errorPassword = null;
        });
    };
})

If I remove the location of course everything works perfectly. I get the error I stated above when I add the location. I believe it has something to do with the promise but I am not sure what.

allencoded
  • 7,015
  • 17
  • 72
  • 126
  • May I suggest checking out http://stackoverflow.com/questions/14301524/in-angular-how-to-redirect-with-location-path-as-http-post-success-callback – miqh May 26 '14 at 07:38

1 Answers1

1

You need to resolve the promise.

factory.register = function (data) {
    return $http.post(urlBase + '/Register', data).then(function(response) {
        if (typeof response.data === "object") {
            $location.path('/login');
            return $q.resolve(response.data);
        } else {
            return $q.reject(response.data);
        }
    }, function(response) {
        return $q.reject(response.data);
    });
};
Sriharsha
  • 2,373
  • 1
  • 16
  • 20