You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

React Native调用Woocommerce REST API获取分类报错及返回条数受限问题

问题原因
  • 返回数据最多10条:Woocommerce REST API默认分页条数为10,你使用的参数传递方式不符合SDK要求,参数未生效,因此返回默认条数。
  • 提示per_page is not type of integer:你直接将参数拼接在请求路径中,SDK解析时会将该值判定为字符串类型,不满足接口要求的整数类型校验规则。
报错响应截图

响应截图

解决方案

修正请求写法

官方Woocommerce REST API SDK的GET方法,查询参数需单独作为第二个入参传入,同时注意你初始化代码中的版本参数名写错了,修正后可同时解决两个问题,代码如下:

const WooCommerce = new WooCommerceRestApi({
  url: "xxxxxs", // 你的店铺地址
  ssl: true,
  consumerKey: "xxxxx", // 你的consumer key
  consumerSecret: "xxxxxx", // 你的consumer secret
  wpAPI: true, // 开启WP REST API
  version: "wc/v3", // 原代码参数名错误,正确key为version
  queryStringAuth: true,
})

// 修正后的请求写法
const categories = await WooCommerce.get("products/categories", {
  per_page: 50
})
console.log(categories)

全量分类拉取说明

Woocommerce REST API限制per_page最大值为100,若你的店铺分类总数超过100,需要通过分页循环拉取的方式获取全量数据,参考逻辑如下:

let allCats = []
let currentPage = 1
const pageSize = 100

while (true) {
  const { data: currentCats } = await WooCommerce.get("products/categories", {
    per_page: pageSize,
    page: currentPage
  })
  allCats.push(...currentCats)
  if (currentCats.length < pageSize) break
  currentPage++
}

console.log("全量分类列表:", allCats)

内容的提问来源于stack exchange,提问作者lina

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.09.29 13:06:04