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?
To resolve your TypeError: Cannot read properties of undefined (reading '0') , go through these steps: Ensure you are using the correct variable. Perform a simple check on your variable before using it to make sure it is not undefined. Create a default value for the variable to use if it does happen to be undefined.
JavaScript TypeError is thrown when an operand or argument passed to a function is incompatible with the type expected by that operator or function. This error occurs in Chrome Browser when you read a property or call a method on an undefined object .
The "Cannot read property 'click' of null" error occurs when trying to call the click method on a null value. To solve the error, run the JS script after the DOM elements are available and make sure you only call the method on valid DOM elements.
Promise resolve() method: Any of the three things can happened: If the value is a promise then promise is returned. If the value has a “then” attached to the promise, then the returned promise will follow that “then” to till the final state. The promise fulfilled with its value will be returned.
Your 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();
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With