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

24,556

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();
    }
Share:
24,556
Paul Erdos
Author by

Paul Erdos

Updated on July 17, 2022

Comments

  • Paul Erdos
    Paul Erdos almost 2 years

    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?