最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Promise - TypeError: Cannot read property 'then' of undefined - Stack Overflow

programmeradmin1浏览0评论

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 the makePayment().then(). – ste2425 Commented Aug 1, 2016 at 7:43
Add a ment  | 

1 Answer 1

Reset to default 3

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();
    }
发布评论

评论列表(0)

  1. 暂无评论