简体   繁体   English

该程序有效,但我收到错误 Object is of type 'unknown'.ts(2571) at the bottom cart[1]

[英]The program works but I'm getting an error Object is of type 'unknown'.ts(2571) at the bottom cart[1]

The error happens at if cart[1] The cart[1] is underlined and I can't deploy the code to Vercel.错误发生在如果购物车 [1] 购物车 [1] 带有下划线并且我无法将代码部署到 Vercel 时。

Thought it was a stripe problem but I thoroughly checked stripes API and this code make as much sense as I can think of.认为这是一个条纹问题,但我彻底检查了条纹 API,这段代码在我能想到的范围内都很有意义。

here's is the full file.这是完整的文件。 its a.ts extension它的 a.ts 扩展名

import { NextApiRequest, NextApiResponse } from "next";

import Stripe from "stripe";
const stripe = new     Stripe(process.env.STRIPE_SECRET_KEY!, {
  apiVersion: "2020-03-02",
});

export default async function handler(
  req: NextApiRequest,
  res: NextApiResponse
) {
  if (req.method === "POST") {
    try {
      const cartItems = req.body;
      const line_items = validateCartItems(cartItems);
      const subscriptionInCart = isSubscriptionInCart(cartItems);
      const params: Stripe.Checkout.SessionCreateParams = {
        mode: subscriptionInCart ? "subscription" : "payment",
        payment_method_types: ["card"],
        billing_address_collection: "auto",
        shipping_address_collection: {
          allowed_countries: ["US", "CA"],
        },
        line_items,
        success_url: `${req.headers.origin}/result?session_id=.   {CHECKOUT_SESSION_ID}`,
        cancel_url: `${req.headers.origin}/use-shopping-cart`,
      };
      const checkoutSession: Stripe.Checkout.Session =
        await stripe.checkout.sessions.create(params);

      res.status(200).json(checkoutSession);
    } catch (err: any) {
      res.status(500).json({ statusCode: 500, message: err.message });
    }
  } else {
    res.setHeader("Allow", "POST");
    res.status(405).end("Method Not Allowed");
  }
}

const validateCartItems = (cartDetails: any) => {
  const validatedItems = [];
  for (const sku in cartDetails) {
    const product = cartDetails[sku];
    const item = {
      price: product.sku,
      quantity: product.quantity,
    };
    validatedItems.push(item);
  }

  return validatedItems;
};

const isSubscriptionInCart = (cartDetails: any) => {
  let subscriptionFound = false;
  for (const cartItem of Object.entries(cartDetails)) {
    if (cartItem[1].recurring) {
      subscriptionFound = true;
    }
  }
  return subscriptionFound;
};

As @juliomalves mentioned, your cartDetails is correctly identifying a type error.正如@juliomalves 提到的,您的cartDetails正在正确识别类型错误。

You need to specify a type for cartDetails so that cartItem[1].recurring is expected to be defined in a way that matches with your use of it.您需要为cartDetails指定一个类型,以便cartItem[1].recurring的定义方式与您对它的使用相匹配。

Something like const isSubscriptionInCart = (cartDetails: Array<{recurring: boolean}>) => {...} or better yet defining your own type CartItem and using Array<CartItem>类似const isSubscriptionInCart = (cartDetails: Array<{recurring: boolean}>) => {...}或更好的是定义您自己的type CartItem并使用Array<CartItem>

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM