【问题标题】:NextJs & Strapi using getStaticPaths errorNextJs & Strapi 使用 getStaticPaths 错误
【发布时间】:2021-10-09 21:17:00
【问题描述】:

目前我正在使用 Strapi 为前端构建自定义 API 和 NextJs,我正在尝试使用 getStaticPaths 来创建基于类别的页面。我已经在 Strapi 中设置了一个与我的论文集合相关的类别集合,并且在使用 Postman 测试 API 路由时一切正常。但是,当我尝试访问应该 http://localhost:3000/categories/1getStaticPaths 路由时,Next 给了我一个错误,但我得到了错误 Error: A required parameter (category) was not provided as a string in getStaticPaths for /categories/[category] 目前我的代码如下所示;但是我很困惑,因为我将它转换为一个应该纠正错误的字符串?顺便说一句,我不是 NextJs 的专业人士。

如果我在 Postman 或浏览器中手动输入路由,它会正常工作,并以正确的 json 输出响应。 Strapi 的控制台也显示了发送的请求,但是当 Next 尝试加载我猜的页面时,控制台中没有显示,因为它没有走那么远。

我该如何解决上面提到的错误我在这里已经好几天了,它变得有点烦人了哈哈

// pages/categories/[category].js

function Category({ categories }) {
 return (
    <>
    <h1>{categories.name}</h1>
    <h2>{categories.papers.name}</h2>
    </>
   )
  }
  
  export async function getStaticPaths() {
    const res = await fetch('http://localhost:1337/Categories')
    const Categories = await res.json()
    await console.log(Categories);
  
    const paths = Categories.map((category) => ({
      params: { id: category.id.toString() },
    }))

    return { paths, fallback: false }
  }
  

  export async function getStaticProps({ params }) {
    // params contains the post `id`.
    // If the route is like /posts/1, then params.id is 1
    const res = await fetch(`http://localhost:1337/Categories/${params.id}`)
    const categories = await res.json()
    console.log(categories)
  
    // Pass post data to the page via props
    return { props: { categories } }
  }
  
  export default Category

http://localhost:1337/Categories/**${params.id}** 的正确响应 - 应该是 1,表示 url 是 http://localhost:1337/Categories/1

{
    "id": 2,
    "name": "English",
    "published_at": "2021-10-08T10:12:08.041Z",
    "created_at": "2021-10-08T10:12:04.011Z",
    "updated_at": "2021-10-08T10:12:08.061Z",
    "papers": [
        {
            "id": 1,
            "name": "2020 English Studies (Testing)",
            "description": "# Testing",
            "PDF_link": "/uploads/2020_hsc_english_studies_98eabce6e7.pdf",
            "published_at": "2021-10-08T10:14:55.816Z",
            "created_at": "2021-10-08T10:12:48.714Z",
            "updated_at": "2021-10-08T10:14:55.835Z",
            "Media_Upload": [
                {
                    "id": 1,
                    "name": "2020-hsc-english-studies.pdf",
                    "alternativeText": "",
                    "caption": "",
                    "width": null,
                    "height": null,
                    "formats": null,
                    "hash": "2020_hsc_english_studies_98eabce6e7",
                    "ext": ".pdf",
                    "mime": "application/pdf",
                    "size": 4959.79,
                    "url": "/uploads/2020_hsc_english_studies_98eabce6e7.pdf",
                    "previewUrl": null,
                    "provider": "local",
                    "provider_metadata": null,
                    "created_at": "2021-10-08T10:14:32.827Z",
                    "updated_at": "2021-10-08T10:14:32.847Z"
                }
            ]
        }
    ]
}

【问题讨论】:

    标签: javascript api next.js strapi


    【解决方案1】:

    params 中的键应与您的动态路由名称相对应。您在那里传递id 键,但您的路线称为/categories/[category],因此您需要传递category 键。

        const paths = Categories.map((category) => ({
          params: { category: category.id.toString() },
        }))
    

    getStaticProps 中显然还从参数中获取category

    【讨论】:

    • 您能解释一下将其更改为 /Math 而不是 /1 的方法吗?那是下一个任务
    • 只需路径名称而不是 id 为 category。但是你的 api 应该能够按名称找到这个实体(你在getStaticProps 中进行的调用)。文档对所有内容都有很好的解释,nextjs.org/docs/basic-features/…
    猜你喜欢
    • 2022-11-02
    • 2021-08-11
    • 2021-04-23
    • 2020-09-28
    • 2021-11-28
    • 2021-09-20
    • 2022-11-02
    • 2021-11-13
    • 2020-10-18
    相关资源
    最近更新 更多