简体   繁体   English

带节点的条带引发错误 400:“无法读取未定义的属性‘会话’”

[英]Stripe with Node throws Error 400: "Cannot read property 'sessions' of undefined"

I'm trying to integrate stripe to my organization's app, however, following stripe's own documentation, the way to integrate using nodeJS is by doing this:我正在尝试将条带集成到我组织的应用程序中,但是,按照条带自己的文档,使用 nodeJS 进行集成的方法是这样做:

https://stripe.com/docs/billing/subscriptions/checkout#create-session https://stripe.com/docs/billing/subscriptions/checkout#create-session

I tried pretty much everything, but the server always throws a 404 error with the following message: "Cannot read property 'sessions' of undefined"我几乎尝试了所有方法,但服务器总是抛出 404 错误并显示以下消息:“无法读取未定义的属性‘会话’”

The thing is that when I console the stripe object in the backend I see that in fact there is no 'checkout' property for stripe, but the stripe docs say it does have it, which leads me to think that I must be doing something wrong.问题是,当我在后端控制条带对象时,我发现实际上条带没有“结帐”属性,但条带文档说它确实有它,这让我认为我一定做错了什么.

This is my "go to checkout" button's frontend code这是我的“去结帐”按钮的前端代码

    const createCheckoutSession = (priceId:any) => {
        return axios.post("/create-checkout-session", {
          method: "POST",
          headers: {
            "Content-Type": "application/json"
          },
          body: JSON.stringify({
            priceId: priceId
          })
        }).then(function(result) {
          return result.data();
        });
      };
    
    const handleClick = async (event:any) => {
        // Get Stripe.js instance
        const stripe = await stripePromise;
    
        // Call your backend to create the Checkout Session
        //const response = await fetch('/create-checkout-session', { method: 'POST' });
        createCheckoutSession('price_1J7UmZFYFs5GcbAXvPronXSX').then(function(data) {
            // Call Stripe.js method to redirect to the new Checkout page
            stripe
              .redirectToCheckout({
                sessionId: data.sessionId
              })
              .then(()=>{console.log('handleResult')});
          })
          };

And here is my server.js code这是我的 server.js 代码

//STRIPE CODE
const stripeKey = 'sk_test_51J7UfrFYFs5GcbAX45lXy6w2TV1hPqvUbqu3hdB4QRAXFR3QYmTgcNKXcVG7tL9vwaanjdYWGvkfiQ6Bd41dK4V7004yMs3Cbh'
const stripe = Stripe(stripeKey);

app.post('/create-checkout-session', async (req, res) => {
  //console.log("--------STRIPE----------", stripe);
  const { priceId } = req.body;

  try {
    const session = await stripe.checkout.sessions.create({
      mode: 'subscription',
      payment_method_types: ['card'],
      line_items: [
        {
          price: priceId,
          // For metered billing, do not pass quantity
          quantity: 1,
        },
      ],
      // {CHECKOUT_SESSION_ID} is a string literal; do not change it!
      // the actual Session ID is returned in the query parameter when your customer
      // is redirected to the success page.
      success_url: 'https://localhost:3000/success?session_id={CHECKOUT_SESSION_ID}',
      cancel_url: 'https://localhost:3000/canceled',
    });

    res.send({
      sessionId: session.id,
    });
  } catch (e) {
    res.status(400);
    return res.send({
      error: {
        message: e.message,
      }
    });
  }
});

It sounds like you're using an older version of the Stripe Node library that does not have support for Checkout Sessions.听起来您使用的是不支持结帐会话的旧版 Stripe 节点库。 You should check the version of the library you're using an upgrade to the latest if possible, or v6.20.0 at minimum (the first version to add Checkout support).如果可能,您应该检查您使用的库的 版本升级到最新版本,或者至少是v6.20.0 (添加 Checkout 支持的第一个版本)。

Alright guys, so I figured it out, stripe was already installed in my packages, however every time I tried to update it it would say I was on the latest version.好吧,所以我想通了,我的包中已经安装了条带,但是每次我尝试更新它时,它都会说我使用的是最新版本。

Anyway, I uninstalled and then reinstalled everything stripe related again using npm i <package> and it suddenly worked just fine无论如何,我卸载然后使用npm i <package>重新安装了所有与 stripe 相关的东西,它突然工作得很好

I uninstalled stripe, reinstalled - no dice.我卸载了条纹,重新安装 - 没有骰子。

Make sure that, according to the docs:确保根据文档:

The package needs to be configured with your account's secret key, which is available in the Stripe Dashboard.该软件包需要使用您帐户的密钥进行配置,该密钥可在 Stripe Dashboard 中找到。 Require it with the key's value:要求它带有键的值:

Like喜欢

const stripe = require('stripe')(process.env.STRIPE_PRIVATE_KEY);

After ensuring that, everything was fine.确保之后,一切都很好。

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

相关问题 JavaScript抛出“无法读取未定义的属性……”错误 - JavaScript throws 'cannot read property … of undefined' error 类型错误:无法读取未定义的属性“forEach”(节点 js、stripe、express) - TypeError: Cannot read property 'forEach' of undefined (node js, stripe, express) @rollup/plugin-node-resolve 抛出错误:无法读取未定义的属性“长度” - @rollup/plugin-node-resolve throws error: Cannot read property 'length' of undefined 节点错误“无法读取未定义的属性&#39;insertId&#39;” - Node error “ Cannot read property 'insertId' of undefined” 节点错误:无法读取未定义的属性 - Node error: cannot read property of undefined 节点错误无法读取未定义的属性“解析” - Node error Cannot read property 'resolve' of undefined TypeError:无法读取未定义的属性“会话” - TypeError: Cannot read property 'sessions' of undefined Knockback Collection Observable抛出“无法读取未定义的属性&#39;bind&#39;”错误 - Knockback Collection Observable throws “Cannot read property 'bind' of undefined” error 传单 setLatLngs 函数抛出错误:“无法读取未定义的属性‘应用’” - leaflet setLatLngs function throws the error: "Cannot read property 'apply' of undefined" 导入require()会引发错误:无法读取未定义的属性&#39;createsocket&#39; - require() to import throws error: Cannot read property 'createsocket' of undefined
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM