【发布时间】:2021-12-08 17:12:20
【问题描述】:
我在 getServerSideProps 函数中的 http axios api 数据获取尝试总是返回错误。我已成功从 cockies 中恢复 token 和 userId,并尝试将它们作为参数传递以进行服务器 api 调用。
export const getServerSideProps: GetServerSideProps = async (ctx) => {
try {
const { userId, token } = ctx.req.cookies;
// console.log(userId)
// console.log(token)
const res = await api.get(`/users/show/${userId}`, {
headers: { token },
})
console.log(res.data)
const userData = res.data;
if (!userData) {
return {
notFound: true,
}
}
return {
props: {
userData
}
}
} catch (error) {
return error
}
}
并不断收到同样的错误:
Server Error
Error: Additional keys were returned from `getServerSideProps`. Properties intended for your component must be nested under the `props` key, e.g.:
return { props: { title: 'My Title', content: '...' } }
Keys that need to be moved: config, request, response, isAxiosError, toJSON.
【问题讨论】:
-
return { notFound: true, } 返回 { props: { notFound: true } }
-
这是什么
await res.data; -
我添加了它,同样的错误仍在继续
-
@Djony 在 catch 块上添加
return { props: {} } -
return error与return { props: {} }不同,您不能只返回任何未包裹在道具之间的对象
标签: axios next.js fetch-api getserversideprops