首页 > 解决方案 > 有没有人遇到过这个错误:Uncaught TypeError: Cannot read property 'createPaymentMethod' of undefined

问题描述

我目前正在使用 Stripe.js V2 和 V3,我需要使用 stripe.createPaymentMethod() 方法。当我尝试使用以下代码时,出现错误,如下所示:

      const {paymentMethod, error} = await stripe.createPaymentMethod({
        type: 'card',
        card: this.refs.creditCardForm,
        billing_details: {
          name: 'Jenny Rosen',
        },
      });

错误:未捕获的类型错误:无法读取未定义的属性“createPaymentMethod”

顺便说一句,当我使用 JS 而不是 React 时,我得到了同样的错误:

     Stripe.createPaymentMethod({
        type: 'card',
        card: cardElement,
        billing_details: {
          name: values.payment.creditCardHolderName,
        }
      }).then(function(result) {
        // handle result
      });

任何信息/经验表示赞赏。

标签: reactjsstripe-paymentspayment-processing

解决方案


我在为我的组件创建单元测试时遇到了这个问题。我在调用 createPaymentMethod 之前添加了一个验证 if(this.stripe) 并解决了这个问题。希望能帮助到你。


推荐阅读