【问题标题】:Next.js: getStaticPaths for nested dynamic routes data structure errorNext.js:嵌套动态路由数据结构错误的getStaticPaths
【发布时间】:2020-11-20 13:44:34
【问题描述】:

我有一个页面 [categories][price].js,我试图在 getStaticPaths 中实现数据结构,例如 cat1/10 cat1/20 cat1/30 cat1/40 cat2/10 cat/20 等

我看过这篇文章:Next.js: getStaticPaths for nested dynamic routes,因为它是相同的错误,但由于它们的数据结构有点不同,我不知道如何将其转换为我的示例

我在尝试创建动态路由时遇到以下错误,因此我似乎错误地映射了数据。

Error: Additional keys were returned from `getStaticPaths` in page "/[catSlug]/[price]". URL Parameters intended for this dynamic route must be nested under the `params` key, i.e.:

    return { params: { catSlug: ..., price: ... } }

Keys that need to be moved: 0, 1.

如何正确映射我的数据?

[类别][价格].js

export async function getStaticPaths() {
   const prices = [' 10', ' 20', ' 30']
  const categories = [{ name: 'cat' }, { name: 'cat2' }, { name: 'cat3' }]
  const paths = categories.map(({ slug }) =>
    prices.map((price) => ({ params: { catSlug: slug, price: price } }))
  )
  return {
    paths,

    fallback: false
  }
}

【问题讨论】:

    标签: javascript reactjs next.js


    【解决方案1】:

    展平数组

    const paths = categories.map(({ name }) =>
      prices.map((price) => ({ params: { catSlug: name, price: price } }))
    ).flat()
    

    【讨论】:

    • 然后我得到错误 Server Error Error: A required parameter (price) was not provided as a string in getStaticPaths for /[catSlug]/[price]
    • @tomharrison 将slug 改为name 并做console.log(paths) 看看有没有问题
    猜你喜欢
    • 2021-01-29
    • 2023-03-09
    • 1970-01-01
    • 2021-09-28
    • 2023-02-10
    • 2021-12-02
    • 2020-11-05
    • 2020-08-27
    • 2022-07-29
    相关资源
    最近更新 更多