英文:
Why the request to NextJS api route inside app directory gives 500 response code?
问题
When I try to call my API route I receive a 500 code Internal Server Error
Place of making the request:
export const fetchSuggestion = async () => {
const response = await fetch('/api/getSuggestion', { cache: 'no-store' })
const data = await response.json()
return data
}
Place of calling the fetcher:
const {
data: suggestion,
isLoading,
isValidating,
mutate,
} = useSWR('/api/getSuggestion', fetchSuggestion, { revalidateOnFocus: false })
Api route itself:
export async function GET(request: Request) {
// Connect to mcrft azure func endpoint
const response = await fetch(`${process.env.VERCEL_URL || 'http://localhost:7071'}/api/getChatGPTSuggestion`, {
cache: 'no-store'
})
const textData = await response.text()
return new Response(JSON.stringify(textData.trim()), {
status: 200
})
}
File structure:
app > api > getSuggestion > route.ts
lib > fetchSuggestion.ts
I tried to change the app/api folder with pages/api like in the previous version of Next, but it didn't work. I checked if everything inside the route is okay, and it is, but I don't really understand why it gives me a 500 code.
英文:
When I try to call my API route I receive a 500 code Internal Server Error
Place of making the request
export const fetchSuggestion = async () => {
const response = await fetch('/api/getSuggestion', { cache: 'no-store' })
const data = await response.json()
return data
}
Place of calling the fetcher
const {
data: suggestion,
isLoading,
isValidating,
mutate,
} = useSWR('/api/getSuggestion', fetchSuggestion, { revalidateOnFocus: false })
Api route itself
export async function GET(request: Request) {
// Connect to mcrft azure func endpoint
const response = await fetch(`${process.env.VERCEL_URL || 'http://localhost:7071'}/api/getChatGPTSuggestion`, {
cache: 'no-store'
})
const textData = await response.text()
return new Response(JSON.stringify(textData.trim()), {
status: 200
})
}
File sctructure
app>api>getSuggestion>route.ts
lib>fetchSuggestion.ts
I tried to change the app/api folder with pages/api like in previous version of Next, but it didn't work, checked if all inside the route is ok, and it is, but I don't really understand why it gives me 500 code.
答案1
得分: 1
最可能是你的获取逻辑出错了:
```typescript
export async function GET(request: Request) {
try {
// 连接到 mcrft azure func 端点
const response = await fetch(
`${
process.env.VERCEL_URL || "http://localhost:7071"
}/api/getChatGPTSuggestion`,
{
cache: "no-store",
}
);
const textData = await response.text();
return new Response(JSON.stringify(textData.trim()), {
status: 200,
});
} catch (error) {
console.log("get 路由内部错误", error);
if (error instanceof Error) {
return new Response(error.message, { status: 500 });
}
return new Response("内部服务器错误", { status: 500 });
}
}
<details>
<summary>英文:</summary>
most likely your fetching logic is failing:
const response = await fetch(`${process.env.VERCEL_URL || 'http://localhost:7071'}/api/getChatGPTSuggestion`, {
cache: 'no-store'
})
you should write the logic inside `try/catch` block
export async function GET(request: Request) {
try {
// Connect to mcrft azure func endpoint
const response = await fetch(
`${
process.env.VERCEL_URL || "http://localhost:7071"
}/api/getChatGPTSuggestion`,
{
cache: "no-store",
}
);
const textData = await response.text();
return new Response(JSON.stringify(textData.trim()), {
status: 200,
});
} catch (error) {
console.log("error inside get route",error)
if (error instanceof Error) {
return new Response(error.message, { status: 500 });
}
return new Response("Internal Server Error", { status: 500 });
}
}
</details>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论