Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Promise - TypeError: Cannot read property 'then' of undefined

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?

like image 923
Paul Erdos Avatar asked Oct 01 '14 21:10

Paul Erdos


People also ask

How do you fix TypeError Cannot read property of undefined?

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.

What does Cannot read properties of undefined mean?

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 .

How do you fix TypeError Cannot read properties of null?

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.

How do you resolve a Promise?

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.


1 Answers

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();
    }
like image 97
Shomz Avatar answered Sep 29 '22 14:09

Shomz