我有一个要获取的JSON API页面。如果我搜索获取正确JSON的链接,则将显示所有内容。根据我的获取方式,会返回两个响应之一。我正在使用同构抓取方法。
1。。如果我不使用res.json()并仅返回获取响应,则状态为200且具有不相关的JSON(有些期望,但我仍然想知道为什么我的JSON主体是不在那里。
2。。我觉得这值得注意。如果我在没有getInitialProps或async-await的情况下进行常规提取,则会收到预期的响应,但我需要传递给props,以便如此……
3。。如果我包含res.json(),则会收到此错误...
{ FetchError: invalid JSON response body at http://localhost:5000/api/user/personal reason: Unexpected end of JSON input
在这里输入我的代码...
const Profile = props => {
console.log(props)
return (
<div>
<Navbar />
Profile
{props.username}
</div>
)
}
Profile.getInitialProps = async function () {
const res = await fetch("http://localhost:5000/api/user/personal")
const data = await res.json()
return { data }
}
谢谢,伙计们!即使您没有答案,我也会觉得我在某个地方缺少一个重要的概念,因此任何见识都值得赞赏。