I think I just need another pair of eyes on this, because I can't get what I'm missing here.
$scope.checkout = function (form) {
//some code here
function checkoutErrorHandler(error) {
//some code here
}
function displaySuccessMessage() {
$scope.success = true;
cartService.emptyCart();
}
checkoutService.makePayment($scope.payment).then(function (i) {
//some code here
checkoutService.buyProducts($scope.payment, products, i).then(function () {
displaySuccessMessage().then(function(){
$scope.payment = {}; // clear checkout form
$scope.form.reset();
});
return displaySuccessMessage;
},
checkoutErrorHandler
);
}, checkoutErrorHandler);
};
I get "Cannot read property 'then' of undefined" when I call displaySuccessMessage. I've tried refactoring several different ways but cannot get it to work. Does anyone see my mistake?
I think I just need another pair of eyes on this, because I can't get what I'm missing here.
$scope.checkout = function (form) {
//some code here
function checkoutErrorHandler(error) {
//some code here
}
function displaySuccessMessage() {
$scope.success = true;
cartService.emptyCart();
}
checkoutService.makePayment($scope.payment).then(function (i) {
//some code here
checkoutService.buyProducts($scope.payment, products, i).then(function () {
displaySuccessMessage().then(function(){
$scope.payment = {}; // clear checkout form
$scope.form.reset();
});
return displaySuccessMessage;
},
checkoutErrorHandler
);
}, checkoutErrorHandler);
};
I get "Cannot read property 'then' of undefined" when I call displaySuccessMessage. I've tried refactoring several different ways but cannot get it to work. Does anyone see my mistake?
Share asked Oct 1, 2014 at 21:53 Paul ErdosPaul Erdos 1,3752 gold badges23 silver badges49 bronze badges 3-
5
The "displaySuccessMessage" does not include a
return
statement. – Pointy Commented Oct 1, 2014 at 21:55 -
1
What
Pointy
said,.then
isn't magic, you need to return a promise if you want to use it. I think this is just a typo here so voting to close. – Benjamin Gruenbaum Commented Oct 1, 2014 at 21:57 -
Just FYI As both use the same error handler you could use just a single
.catch()
after themakePayment().then()
. – ste2425 Commented Aug 1, 2016 at 7:43
1 Answer
Reset to default 3Your displaySuccessMessage
does not return a promise. In fact, it doesn't return anything.
Assuming that cartService.emptyCart()
returns a promise, you can modify displaySuccessMessage
like this and it should work just fine:
function displaySuccessMessage() {
$scope.success = true;
return cartService.emptyCart();
}