Next.js Router.push 不设置任何 req.headers
Next.js Router.push does not set any req.headers
我在从另一个端点执行客户端 Router.push() 后在 getInitialProps 函数中执行后端查询时遇到问题。
这就是我的意思。在我的注册页面中,我调用 Router.push('/') 到 return 到主页:
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('/') 时 headers 是未定义的。这是怎么回事,我如何在上下文中将 headers 送入我的请求 object?
使用Router.push时没有请求。它是客户端 API,并且请求仅存在于初始服务器呈现中。
我在从另一个端点执行客户端 Router.push() 后在 getInitialProps 函数中执行后端查询时遇到问题。
这就是我的意思。在我的注册页面中,我调用 Router.push('/') 到 return 到主页:
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('/') 时 headers 是未定义的。这是怎么回事,我如何在上下文中将 headers 送入我的请求 object?
使用Router.push时没有请求。它是客户端 API,并且请求仅存在于初始服务器呈现中。