如何修复"Uncaught (in promise) SyntaxError: Unexpected end of JSON input"?



当我尝试从localhost登录到我的网站时,它运行得很好。然而,当我尝试从我的生产构建中进行同样的操作时,它并不起作用。我想我知道为什么,但我不知道如何修复它。当我试图从prod构建(托管在Vercel上(登录时,它不会发布到https://fs-dev.vercel.app/api/auth/login但是https://fs-dev.vercel.app/undefined/api/auth/login.我不知道它为什么在产品构建中添加和/undefined/。(我使用的是Next.js和他们的API路由(。我已经检查了baseUrl是否正确,因为prod构建能够获取数据,但不能POST数据。

这是我发布请求的代码:

fetchData.js

export const postData = async (url, post, token) => {
const res = await fetch(`${baseUrl}/api/${url}`, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Authorization': token
},
body: JSON.stringify(post)
})
const data = await res.json()
return data
}

signin.js

const res = await postData('auth/login', userData);

问题是,客户端需要env变量BASE_URL。为了解决这个问题,我在BASE_URL 前面添加了NEXT_PUBLIC_

最新更新