简体   繁体   English

未处理的拒绝(TypeError):无法读取未定义的 Stripe Javascript 的属性“id”

[英]Unhandled Rejection (TypeError): Cannot read property 'id' of undefined Stripe Javascript

I have been attempting to add stripe into an app I am trying to get an MVP of at the moment.我一直在尝试将条纹添加到我目前正在尝试获得 MVP 的应用程序中。 Whenever I type in the test credit card and click send I get sent to a screen with the following error code.每当我输入测试信用卡并单击发送时,我都会被发送到带有以下错误代码的屏幕。

Error Code错误代码

Unhandled Rejection (TypeError): Cannot read property 'id' of undefined
CheckoutForm._callee$
src/components/CheckoutForm.js:17
  14 |       let response = await fetch("/charge", {
  15 |         method: "POST",
  16 |         headers: {"Content-Type": "text/plain"},
> 17 |         body: token.id
  18 |       });
  19 | 
  20 |       if (response.ok) this.setState({complete: true});

Server.js服务器.js

 const app = require("express")();
 const stripe = require("stripe")("STRIPE_SECRET_KEY");
 app.use(require("body-parser").text());
 app.post("/charge", async (req, res) => {
    try {
      let {status} = await stripe.charges.create({
           amount: 2000,
           currency: "usd",
           description: "An example charge",
           source: req.body
         });

           res.json({status});
         } catch (err) {
           res.status(500).end();
         }
       });
       app.listen(9000, () => console.log("Listening on port 9000"));

CheckoutForm.js结帐表格.js

import React, {Component} from 'react';
import {CardElement, injectStripe} from 'react-stripe-elements';


class CheckoutForm extends Component {
  constructor(props) {
     super(props);
     this.state = {complete: false};
     this.submit = this.submit.bind(this);
  }

  async submit(ev) {
    let {token} = await this.props.stripe.createToken({name: "Name"});
    let response = await fetch("/charge", {
      method: "POST",
      headers: {"Content-Type": "text/plain"},
      body: token.id
    });

    if (response.ok) this.setState({complete: true});
  }

render() {
   if (this.state.complete) return <h1>Purchase Complete</h1>;

return (
  <div className="checkout">
     <p>Would you like to complete the purchase?</p>
     <CardElement />
     <button onClick={this.submit}>Send</button>
 </div>
   );
 }
}

export default injectStripe(CheckoutForm);

App.js应用程序.js

import {Elements, StripeProvider} from 'react-stripe-elements';
import CheckoutForm from './components/CheckoutForm';

  <StripeProvider apiKey="STRIPE_PUBLISHABLE_KEY">
    <div className="example">
      <h1>React Stripe Elements Example</h1>
      <Elements>
        <CheckoutForm />
      </Elements>
    </div>
  </StripeProvider>

I am not the best and haven't been able to find anything to help me so far.我不是最好的,到目前为止还没有找到任何可以帮助我的东西。 Please and Thank you谢谢,麻烦您了

createToken can return an error, but it doesn't reject the promise(some discussion here ), instead the returned token will be null. createToken 可以返回错误,但它不会拒绝承诺( 这里有一些讨论),而是返回的令牌将为空。 It's necessary to handle the error like this :有必要像这样处理错误:

this.props.stripe.createToken({name : 'Name').then(({token, error}) => {
  if (error) {
    // handle error
  } else {
    // handle token
  }
});

If you do this, you should see what the underlying error is.如果你这样做,你应该看到潜在的错误是什么。

From you code it seems that this.props.stripe.createToken does not return an object with property token as you are destructuring the response.从您的代码看来, this.props.stripe.createToken在您解构响应时不会返回带有属性token的对象。 Its value is undefine right now.它的价值现在是undefine Your response for this.props.stripe.createToken should be like this to work this code ;您对this.props.stripe.createToken响应应该是这样的,以运行此代码;

let response = await this.props.stripe.createToken({name: "Name"});

 response = {
  token: {
   id: 'some id'
  },
  //other properties.
 }

You may need to check createToken function why its not returning right response.您可能需要检查createToken函数为什么它没有返回正确的响应。

Display the error using :使用以下命令显示错误:

this.props.stripe.createToken({name : 'Name'}).then(({token, error}) => {
  if (error) {
    console.log(error);
  } else {
    // handle token
  }
});

Also, in my case, I was putting an australian post code (only 4 digits), where stripe is actually expecting a different format, In order to avoid this, I used this property :另外,就我而言,我放置了一个澳大利亚邮政编码(只有 4 位数字),其中 stripe 实际上期望使用不同的格式,为了避免这种情况,我使用了以下属性:

 <CardElement hidePostalCode={true} />

I hope this helps我希望这有帮助

localhost:3001/charge is not found because there is indeed no such thing. localhost:3001/charge没有找到,因为确实没有这个东西。 Instead you should modify the code to send request to backend.相反,您应该修改代码以将请求发送到后端。 For example:例如:

let response = await fetch("http://localhost:8081/charge", {
      method: "POST",
      headers: {"Content-Type": "text/plain"},
      body: token.id
    });

After that you should see the underlying error.之后,您应该会看到潜在的错误。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 未处理的拒绝(TypeError):无法读取 React.js 中 stripe.confirmCardPayment 中未定义的属性“id” - Unhandled Rejection (TypeError): Cannot read property 'id' of undefined in stripe.confirmCardPayment in React.js 未处理的拒绝(TypeError):无法读取未定义的属性“ id” - Unhandled Rejection (TypeError): Cannot read property 'id' of undefined ReactJS - 未处理的拒绝(TypeError):无法读取未定义的属性“id” - ReactJS - Unhandled Rejection (TypeError): Cannot read property 'id' of undefined 未处理的拒绝(TypeError):无法读取未定义的属性 - Unhandled Rejection (TypeError): Cannot read property of undefined 无法读取未定义未处理拒绝的属性“_id” - Cannot read property '_id' of undefined unhandled Rejection (节点:19502)UnhandledPromiseRejectionWarning:未处理的承诺拒绝(拒绝ID:1):TypeError:无法读取未定义的属性&#39;indexOf&#39; - (node:19502) UnhandledPromiseRejectionWarning: Unhandled promise rejection (rejection id: 1): TypeError: Cannot read property 'indexOf' of undefined 可能未处理 Promise 拒绝(id:0):类型错误:无法读取 react-native-track-player 中未定义的属性“标题” - Possible Unhandled Promise Rejection (id: 0): TypeError: Cannot read property 'title' of undefined in react-native-track-player 未处理的拒绝(TypeError):无法读取未定义的属性“子级” - Unhandled Rejection (TypeError): Cannot read property 'children' of undefined React-未处理的拒绝(TypeError):无法读取未定义的属性“ city” - React- Unhandled Rejection (TypeError): Cannot read property 'city' of undefined ReactJS:未处理的拒绝(TypeError):无法读取未定义的属性“推送” - ReactJS: Unhandled Rejection (TypeError): Cannot read property 'push' of undefined
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM