Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angular/27.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
Apollo resetStore在Angular客户端中不工作_Angular_Graphql_Apollo_Apollo Client_Apollo Server - Fatal编程技术网

Apollo resetStore在Angular客户端中不工作

Apollo resetStore在Angular客户端中不工作,angular,graphql,apollo,apollo-client,apollo-server,Angular,Graphql,Apollo,Apollo Client,Apollo Server,我正在尝试在客户端集成授权令牌。我正在将此令牌传递到中间件中。当用户注销时,重置存储,然后获取新令牌。现在,当我发送新请求时,它仍在发送旧令牌(缓存) 这是我的代码app.module.ts const networkInterface = createNetworkInterface({ uri: "http://localhost:3000/graphql" }); networkInterface.use([ { applyMiddleware(req, next) {

我正在尝试在客户端集成授权令牌。我正在将此令牌传递到中间件中。当用户注销时,重置存储,然后获取新令牌。现在,当我发送新请求时,它仍在发送旧令牌(缓存)

这是我的代码app.module.ts

const networkInterface = createNetworkInterface({
  uri: "http://localhost:3000/graphql"
});

networkInterface.use([
  {
    applyMiddleware(req, next) {
      if (!req.options.headers) {
        req.options.headers = {}; // Create the header object if needed.
      }

      req.options.headers.authorization = localStorage.getItem(AUTH_TOKEN);
      next();
    }
  }
]);

export function provideClient(): ApolloClient {
  return new ApolloClient({
    networkInterface,
    dataIdFromObject: (o: any) => `${o.__typename}-${o.id},`
  });
}
当我注销时,我有这个代码

localStorage.removeItem(AUTH_TOKEN);
this._apollo.getClient().resetStore(); 
然后,当我发出另一个请求时,它仍然在请求头中使用旧令牌


如何使用新令牌更新此内容?

我可能会将中间件更改为:

networkInterface.use([{
    applyMiddleware(req, next) {
        if (!req.options.headers) req.options.headers = {}
        const token = localStorage.getItem('token')
        req.options.headers.authorization = token ? token : null
        next()
    }
}])
注意这里的三元运算符,
token?令牌:空
。这将确保除非应用程序知道令牌,否则无法发送身份验证标头。如果您的客户端仍在发送该令牌,则未正确删除该令牌

您也可以尝试此快速测试:注销后,按F12并在浏览器控制台中键入:
localStorage
,然后查看令牌是否仍在其中

我看不出你的代码,但看起来你有一个名为AUTH_TOKEN的变量,它返回一个类似“TOKEN”或任何你使用的键的字符串。我只想明确指出,您是按键而不是按值删除令牌

添加令牌:
localStorage.setItem('token','e47nes45nysde5nue5nu')

删除令牌:
localStorage.removietem('token')

如果您的代码如下所示:

const AUTH_TOKEN = 'e47nes45nysde5nue5nu'
localStorage.removeItem(AUTH_TOKEN)
然后,这就是为什么它仍然使用auth令牌进行查询。我认为如果它没有找到匹配的密钥从本地存储中删除,它不会在控制台中生成错误