0

I recently started using AngularJS, although I can see some improvements, I can't get my head round the promises. I created a login service that uses Parse framework to enable users to register and a register controller that sends the data from a form to the service. Since signing up takes a few seconds I decided to use a promise to return the current user back to the login controller once he has been signed up. Unfortunately I keep getting this error: Cannot read property 'then' of undefined. Could someone please point me in the right direction?

Controller:

(function () {
'use strict';

angular
    .module('app.register')
    .controller('RegisterController', RegisterController);

RegisterController.$inject = ['$q', 'logger', 'loginservice'];
/* @ngInject */
function RegisterController($q, logger, loginservice) {
    var vm = this;
    vm.title = 'Register'; 
    vm.register = register;     
    vm.user = {
        username: "",
        password: "",
        email: ""
    };

    activate();

    function activate() {
        logger.info('Activated Register View');
    }

    function register() {
        var promise = loginservice.register(vm.user.username, vm.user.email, vm.user.password);

        promise.then(function(userData) {
            console.log("User: ", userData);
        });
    }
}
})();

Service:

(function() {
'use strict';

angular
    .module('app.core')
    .factory('loginservice', loginservice);

loginservice.$inject = ['$http', '$q', 'logger'];
/* @ngInject */
function loginservice($http, $q, logger) {
    var service = {
        // logIn: logIn,
        // logOut: logOut,
        register: register
    };

    return service;

    function initialize() {
        console.log("parse initialized");
    }

    function register(username, email, password) {
        //alert("HELLO FROM " + username + " " + password + " " + email);

        Parse.initialize(appId, parseId);
        var user = new Parse.User();
        user.set("username", username);
        user.set("password", password);
        user.set("email", email);

        $http.get(user.signUp(null, {
         success: function(user) {
            return user;
          },
          error: function(user, error) {
            return error.message;
            // Show the error message somewhere and let the user try again.
            alert("Error: " + error.code + " " + error.message);
          }
        })).then(function() {
            return user;
        });
    }
}
})();
1
  • you only missed to return $http.get from .register method of service Commented Jun 24, 2015 at 21:20

2 Answers 2

1

You just need to return the $http call:

function register(username, email, password) {
        //alert("HELLO FROM " + username + " " + password + " " + email);

        Parse.initialize(appId, parseId);
        var user = new Parse.User();
        user.set("username", username);
        user.set("password", password);
        user.set("email", email);

        return $http.get(user.signUp(null, {
         success: function(user) {
            return user;
          },
          error: function(user, error) {
            return error.message;
            // Show the error message somewhere and let the user try again.
            alert("Error: " + error.code + " " + error.message);
          }
        })).then(function() {
            return user;
        });
    }

If you don't return the promise that is created during the $http call then a function (by default) returns undefined.

Sign up to request clarification or add additional context in comments.

1 Comment

Haha, perfect, thanks a lot that return statement problably saved me a lot of digging around! :) I will mark this as an answer, you answered it so quickly I can't do it for another 8 minutes.
0

Just return the $http call like so

function register(username, email, password) {
    ...
    return $http.get(user.signUp(null, {
     success: function(user) {
        return user;
      },
      error: function(user, error) {
        return error.message;
        // Show the error message somewhere and let the user try again.
        alert("Error: " + error.code + " " + error.message);
      }
    })).then(function() {
        return user;
    });
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.