reactjs - 有没有人遇到过这个错误: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
});
任何信息/经验表示赞赏。
解决方案
我在为我的组件创建单元测试时遇到了这个问题。我在调用 createPaymentMethod 之前添加了一个验证 if(this.stripe) 并解决了这个问题。希望能帮助到你。
推荐阅读
- scala - 如何从日期字符串中提取年份?
- ruby-on-rails - Rails 5.2 用于数组不包含的控制器
- python-3.x - matplotlib 版本 >=3 不包括 find()
- python - 如果我在编译时不知道属性名称是什么,如何设置 Python 类的属性。?
- apache-spark - 配置 spark-submit 到远程 AWS EMR 集群
- ios - 如何在扩展中合并/访问 pod 文件 (MMWormhole)?
- sql - 从列的每一行中提取部分字符串
- c# - 如何将 C# 字典输出为 C# 代码?
- java - 当我们使用自定义哈希函数时,Java HashTable 是否遵守负载因子(我们指定)?
- elasticsearch - logstash 时间表 - 是否可以延长时间,以便时间表不会在每分钟的第 1 秒开始?