小编chu*_*kau的帖子

Next.js 中的 getServerSideProps() (使用 App Router)

我尝试接触 Next.js 13(使用 App Router)并遇到了一些问题。我的测试应用程序的结构是:

---/school
------/app
   ------/layout.tsx
   ------/page.tsx
---/src
Run Code Online (Sandbox Code Playgroud)

./app/page.tsx是

async function Page() {

    const hotels = await hotelService.getAllHotels()

    return (
        <RootLayout>
            <HomePage hotels={hotels}/>
        </RootLayout>
    )
}

export default Page
Run Code Online (Sandbox Code Playgroud)

当下一个构建进行收集页面数据步骤时, hotelService.getAllHotels()通过 URL 地址从 API 请求数据。URL 地址由$API_BACKEND_ENV变量定义,并取决于环境(URL_FRO_DEV=https://1.1.1.1/hotel/all 和 URL_FRO_PROD=https://2.2.2.2/hotel/all)。

该错误来自const res = wait fetch(this.url + "/hotel/all", { cache: 'no-store' })行。如果this.url未定义,则会引发错误。

如何避免收集页面数据并抛出异常?

NEXT JS文档表示getServerSideProps ()不会在下一个构建命令中启动。我尝试在getServerSideProps()内定义hotelService.getAllHotels ()并期望不请求 API。但这不起作用。

async function …
Run Code Online (Sandbox Code Playgroud)

next.js

2
推荐指数
1
解决办法
1871
查看次数

标签 统计

next.js ×1