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;
});
}
}
})();
$http.getfrom.registermethod of service