【发布时间】:2019-07-06 08:13:38
【问题描述】:
在从另一个端点执行客户端 Router.push() 后,我在 getInitialProps 函数中执行后端查询时遇到问题。
这就是我的意思。在我的注册页面,我调用Router.push('/')返回首页:
proceedToIndex = () => {
Router.push('/');
}
在我的“/”页面中,我调用 getInitialProps 如下:
static async getInitialProps (context) {
try {
if (context.req.headers.cookie) {
const resp = await getUser(context.apolloClient);
if (resp.data.getUser && resp.data.getUser.id) {
return { user: resp.data.getUser };
}
};
} catch(e) {
console.log(e);
}
return { user: undefined };
}
我最终遇到了一个崩溃,说不能为 context.req.headers.cookie 调用未定义的 cookie。因此,当我执行 Router.push('/') 时,标题是未定义的。这里发生了什么?如何在上下文中将标头提供给我的请求对象?
【问题讨论】:
-
.也许helpful..
标签: javascript reactjs next.js