Javascript Stripe handleServerResponse()错误
当我从handleSubmit方法调用handleServerResponse(response)时,一切正常。但是,当我在自己的handleServerResponse函数中调用它时,没有任何参数,正如stripe文档()中所示,我得到了一个错误Javascript Stripe handleServerResponse()错误,javascript,reactjs,stripe-payments,Javascript,Reactjs,Stripe Payments,当我从handleSubmit方法调用handleServerResponse(response)时,一切正常。但是,当我在自己的handleServerResponse函数中调用它时,没有任何参数,正如stripe文档()中所示,我得到了一个错误 handleServerResponse(response) { if (response.error) { // Show error from server on payment form } else if (res
handleServerResponse(response) {
if (response.error) {
// Show error from server on payment form
} else if (response.requires_action) {
// Use Stripe.js to handle required card action
this.props.stripe
.handleCardAction(response.payment_intent_client_secret)
.then(function(result) {
if (result.error) {
// Show error in payment form
} else {
// The card action has been handled
// The PaymentIntent can be confirmed again on the server
return fetch(`http://127.0.0.1:8000/api/create-charge/`, {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({
payment_intent_id: result.paymentIntent.id
})
})
.then(function(confirmResult) {
return confirmResult.json();
}) ////Line 37, Points the error here
.then(this.handleServerResponse);
}
});
} else {
console.log("Success");
}
}
我得到的错误如下:
未捕获(承诺中)TypeError:无法读取未定义的属性“handleServerResponse”
在CheckoutForm.js:37
第37行位于handleServerResponse内
.then(function(confirmResult) {
return confirmResult.json();
}) /// LINE 37
.then(this.handleServerResponse);
我是ReactJS新手,我不明白为什么会发生这种情况,特别是因为代码来自Stripe文档。我认为可能需要用括号调用函数,但这也不起作用
有没有关于如何解决这个问题的建议?非常感谢
--编辑:为了修复它,我必须将其绑定到函数,如下所示:
if (result.error) {
// Show error in payment form
} else {
// The card action has been handled
// The PaymentIntent can be confirmed again on the server
return fetch(`http://127.0.0.1:8000/api/create-charge/`, {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({
payment_intent_id: result.paymentIntent.id
})
})
.then(function(confirmResult) {
return confirmResult.json();
})
.then(data => this.handleServerResponse(data));
}
}.bind(this)````
您将
此
设置为未定义,因此会出现错误。尝试将其更改为,然后(data=>this.handleServerResponse(data))代码>查看是否有效,尝试后问题保持不变。为什么这里没有定义,但当我在handleSubmit()函数中使用此.handleServerResponse()时,没有出现这样的错误?还尝试绑定此:this.handleServerResponse=this.handleServerResponse.bind(this)
但是错误持续存在于一个箭头函数中:handleServerResponse=(response){
看看这是否解决了:handleServerResponse=(response)=>{
修复了它!只需要绑定(这个)在另一个地方!我将在主帖子中发布答案。感谢@Rikin指出错误和此有关,我认为这和函数并没有任何参数有关…您有此
未定义,所以出现错误。尝试将其更改为。然后(data=>this.handleServerResponse(data))
看看这是否有效,尝试后问题仍然存在。为什么没有定义这个。但是当我在handleSubmit()函数中使用这个.handleServerResponse()时,没有出现这样的错误?还尝试绑定这个:this.handleServerResponse=this.handleServerResponse.bind(这个)
但是错误持续存在,将handleServerResponse(response){
转换为一个箭头函数,看看这是否可以解决:handleServerResponse=(response)=>{
修复了它!只需要绑定(这个)在另一个地方!我将在主帖子中发布答案。感谢@Rikin指出错误和此有关,我认为它和并没有任何参数的函数有关。。。
if (result.error) {
// Show error in payment form
} else {
// The card action has been handled
// The PaymentIntent can be confirmed again on the server
return fetch(`http://127.0.0.1:8000/api/create-charge/`, {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({
payment_intent_id: result.paymentIntent.id
})
})
.then(function(confirmResult) {
return confirmResult.json();
})
.then(data => this.handleServerResponse(data));
}
}.bind(this)````