简体   繁体   中英

Next.js: getStaticPaths for nested dynamic routes data structure error

I have a page [categories][price].js and im trying to achieve the data structure in getStaticPaths eg cat1/10 cat1/20 cat1/30 cat1/40 cat2/10 cat/20 etc

I have looked at this post: Next.js: getStaticPaths for nested dynamic routes as it's the same error but as their data structure is a bit different I'm not sure how to translate this to my example

It looks like im mapping the data incorrectly as I get the following error when trying to create my 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.

How can I correctly map my data?

[categories][price].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
  }
}

flatten the array

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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