reactjs 条带Axios错误:请求失败,状态代码为500

j8ag8udp  于 2022-12-18  发布在  React
关注(0)|答案(1)|浏览(262)

我正在尝试按照此example.在NextJS中实现条带检出
但我总是得到这个错误:

checkout.js:

import React from "react";
    import Header from "../components/Header";
    import Image from "next/image";
    import { selectItems, selectTotal } from "../slices/basketSlice";

    import { useSelector } from "react-redux";
    import CheckoutProduct from "../components/CheckoutProduct";
    import Currency from "react-currency-formatter";
    import { useSession } from "next-auth/react";
    import {loadStripe} from '@stripe/stripe-js';
    
    import axios from 'axios';
    const stripePromise = loadStripe(process.env.stripe_public_key)
    
    function Checkout() {
      const items = useSelector(selectItems);
      const total = useSelector(selectTotal);
      const { data: session } = useSession();
    
      const CreateCheckoutSession = async () => {
        const stripe = await stripePromise
    
        // create checkout session
        const checkoutSession = await axios.post("/api/create-checkout-session", {
          items : items,
          email: session.user.email,
        })
        //redirect user to Stripe checkout
        const result = await stripe.redirectToCheckout({
            sessionId: checkoutSession.data.id
        })
    
        if (result.error) {
          alert(result.error.message)
        }
      }

create-checkout-session.js(尝试按照文档页面上的方式执行,但结果相同):

const stripe = require('stripe')(process.env.STRIPE_SECRET_KEY)

export default async (req, res) => {
    const {
        items,
        email
    } = req.body

    // console.log(items)
    //console.log(email)
    // console.log(process.env.STRIPE_SECRET_KEY)

    const transformedItems = items.map(item => ({
        description: item.description,
        quantity: 1,
        price_data: {
            currency: 'usd',
            unit_amount: item.price * 100,
            product_data: {
                name: item.title,
                images: [item.image]
            },
        },
    }))

    const session = await stripe.checkout.sessions.create({
        payment_method_types: ['card'],
        mode: 'payment',
        shipping_rates: ['shr_1MFhHmL1z8EG1fYglFH9EBP7'],
        shipping_address_collection: {
            allowed_countries: ['GB', 'US', 'CA']
        },
        line_items: transformedItems,
        success_url: `${pocess.env.HOST}/success`,
        cancel_url: `${process.env.HOST}/checkout`,
        metadata: {
            email,
            images: JSON.stringify(items.map(item => item.image))
        }
    })
}

res.status(err.statusCode || 500).json(err.message);

res.status(200).json({
    id: session.id
})

在我看来,我似乎遵循了Stipe的格式,但只是不断得到这个无益的错误。有人知道这意味着什么吗?

dbf7pr2w

dbf7pr2w1#

错误似乎来自您自己的服务器,而不是Stripe的API。在您的create-checkout-session.js文件中,异步函数的作用域似乎在您运行res.status(xxx).json(...)之前结束。
在本例中,session变量超出了res.status(...).json(...)函数的作用域。另外,我怀疑这段代码发送了500个res.status(err.statusCode || 500).json(err.message);,因为它不知道err变量是什么。
您可以尝试固定作用域并定义err变量,看看是否会有不同?

相关问题