next.js 用gte实现对大数字的支持过滤

1l5u6lss  于 2023-11-18  发布在  其他
关注(0)|答案(1)|浏览(106)

您好吗?我正在使用nextjs页面路由器和supports创建一个商店。当使用过滤器来获取我的产品时,我发现了一个错误。gte过滤器对超过4位的数字不起作用。(例如:11000)
我的API路由是这样的:

async function handler(req: NextApiRequest, res: NextApiResponse) {
  if (req.method !== "POST") {
    // Return an error response for unsupported request methods
    res.status(405).json({ error: "Method Not Allowed" });
    return;
  }

  const {
    minPrice,
    maxPrice,
    oferta,
    garment,
    category,
    search,
  }: {
    minPrice: number | undefined | null;
    maxPrice: number | undefined | null;
    oferta: string | undefined | null;
    garment: string | undefined | null;
    category: string | undefined | null;
    search: string | undefined | null;
  } = req.body;

  let query = supabase
    .from("products")
    .select(
      "seller!inner( status ), id, published, published_version, created_at"
    )
    .eq("suspended", "false")
    .eq("published", "true")
    .eq("seller.status", "live");

  if (oferta === "si") {
    query = query.neq("published_version->>offer_price", "");
  }

  if (oferta === "no") {
    query = query.eq("published_version->>offer_price", "");
  }

  if (category) {
    query = query.eq("published_version->category->>main", category);
  }

  if (garment) {
    query = query.eq("published_version->category->>garment", garment);
  }

  if (minPrice) {
    console.log("minPrice", minPrice);
    query = query.gte("published_version->>price", minPrice);
  }

  // we fetch the products with the filters applied and order them

  query = query.order("created_at", { ascending: false }).limit(50);

  const { data, error } = await query;

  if (error || !data) {
    console.log("ERROR", error);
    res.status(500).json({ error: error.message });
  }

  const products = data?.map((product) => {
    return {
      ...product,
      data: {
        ...product.published_version,
      },
    };
  });

  res.status(200).json({ data: products });
}

字符串
除了minPrice和maxPrice过滤器,一切都很好。我不能过滤大于9999的数字。
有人能帮帮我吗?谢谢!

g2ieeal7

g2ieeal71#

我已经解决了这个问题。显然,我用错了->>。对于jsonb的第一个孩子,我应该使用->

相关问题