0

好的,这可能是一个非常愚蠢的问题,但我以前从未使用过“getStaticPaths”,但对于这个项目,我也真的需要!

我在“getStaticPaths”上使用了 NextJs文档的代码,当我前往应该是路由“http://localhost:3000/1”时,代码编译正确,但它返回 404 not found?我的自定义 API 是使用 Strapi 构建的。使用 Postman 时,它会返回带有 json 响应的 GET 请求,如下所示。

为什么这不起作用?我非常困惑,我们将不胜感激!

他们也是一种将路线更改为“/Math”而不是“/1”的方法吗?

再次提前感谢!


        "id": 1,
        "name": "Math",
        "published_at": "2021-10-08T04:32:51.871Z",
        "created_at": "2021-10-08T04:32:50.777Z",
        "updated_at": "2021-10-08T04:32:51.883Z",
        "papers": [
            {
                "id": 1,
                "name": "Maths Standard 2020 Past Papers 1",
                "description": "# 2020 Maths Standard Past Papers 1\n\nOriginal Source: [Source](https://educationstandards.nsw.edu.au/wps/portal/nesa/resource-finder/hsc-exam-papers/2020/mathematics-standard-2020-hsc-exam-pack)",
                "pdf_gcs_link": "https://storage.cloud.google.com/matsites/2020-hsc-mathematics-standard-1.pdf",
                "published_at": "2021-10-08T04:39:01.111Z",
                "created_at": "2021-10-08T04:38:49.013Z",
                "updated_at": "2021-10-08T05:21:32.293Z",
                "pdf": [
                    {
                        "id": 1,
                        "name": "2020-hsc-mathematics-standard-1(1).pdf",
                        "alternativeText": "",
                        "caption": "",
                        "width": null,
                        "height": null,
                        "formats": null,
                        "hash": "2020_hsc_mathematics_standard_1_1_1e764b3f71",
                        "ext": ".pdf",
                        "mime": "application/pdf",
                        "size": 612.42,
                        "url": "/uploads/2020_hsc_mathematics_standard_1_1_1e764b3f71.pdf",
                        "previewUrl": null,
                        "provider": "local",
                        "provider_metadata": null,
                        "created_at": "2021-10-08T04:37:35.185Z",
                        "updated_at": "2021-10-08T04:37:35.192Z"
                    }
                ]
            }
        ]
    },
    {
        "id": 2,
        "name": "English",
        "published_at": "2021-10-08T04:32:59.966Z",
        "created_at": "2021-10-08T04:32:58.804Z",
        "updated_at": "2021-10-08T04:32:59.974Z",
        "papers": []
    }
]
// pages/posts/[id].js

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

    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()
  
    // Pass post data to the page via props
    return { props: { categories } }
  }
  
  export default Category
  

// categories response
Response {
  size: 0,
  timeout: 0,
  [Symbol(Body internals)]: {
    body: PassThrough {
      _readableState: [ReadableState],
      _events: [Object: null prototype],
      _eventsCount: 2,
      _maxListeners: undefined,
      _writableState: [WritableState],
      allowHalfOpen: true,
      [Symbol(kCapture)]: false,
      [Symbol(kCallback)]: null
    },
    disturbed: false,
    error: null
  },
  [Symbol(Response internals)]: {
    url: 'http://localhost:1337/Categories',
    status: 200,
    statusText: 'OK',
    headers: Headers { [Symbol(map)]: [Object: null prototype] },
    counter: 0
  }
}
4

1 回答 1

0

fetchcall in有错字,getStaticProps后面应该有一个斜杠Categories

const res = await fetch(`http://localhost:1337/Categories/${params.id}`)

您可以通过替换category.idcategory.namein来更改路线getStaticPaths

params: { id: category.name},

但是你必须修改你的fetch呼入getStaticPropsapi端点。更好的方法是使用next.js 重写

于 2021-10-08T08:30:52.600 回答