Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/59.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Next.js 在nextjs中获取上下文_Next.js - Fatal编程技术网

Next.js 在nextjs中获取上下文

Next.js 在nextjs中获取上下文,next.js,Next.js,我正在使用Next.js,我想在\u app.js中获取auth0数据 AppComponent.getInitialProps=async(ctx)=>{ 常数{req,res}=ctx; const tokenCache=auth0.tokenCache(req,res); const{datToken}=wait tokenCache.getAccessToken(); 返回{t:datToken} }在\u app级别上,getInitialProps的第一个参数是AppContext

我正在使用Next.js,我想在
\u app.js
中获取auth0数据

AppComponent.getInitialProps=async(ctx)=>{
常数{req,res}=ctx;
const tokenCache=auth0.tokenCache(req,res);
const{datToken}=wait tokenCache.getAccessToken();
返回{t:datToken}

}
\u app
级别上,
getInitialProps
的第一个参数是
AppContext
类型,它没有
req
res

相反,它有
ctx
,它的类型是
NextPageContext
,并且具有这些属性

所以试着去替换

const{req,res}=ctx

const{req,res}=ctx.ctx


此外,如果愿意,您可以考虑将参数重命名为类似于
appContext

它正在工作,但是当我想要更改路由时,我得到:
错误页面中的错误
getInitialProps
:错误:令牌缓存方法只能从服务器端使用
。但当我改变路线时,我需要在每一页中保留该标记。问题是什么?那么我想你需要检查你的代码是否只在服务器上运行。例如,如果(typeof document==='undefined'){//表示我们在服务器上}您可以通过这样的检查来实现这一点。如果您希望在导航期间保持这种状态,请将其存储在_app.js中的React状态。然后从那里访问它。你能帮我吗?