简体   繁体   English

在 angular 8 上刷新页面不显示来自后端 api 的数据

[英]On angular 8 refreshing a page doesn't show the data from backend api

I am fetching data from backend and showing it in a mat table, this table is rendered on a component inside a lazy loaded module.我正在从后端获取数据并将其显示在 mat 表中,该表在延迟加载模块内的组件上呈现。 Everything seems to work fine until I refresh the page from the browser ( or it gets refreshed on its own while ng serve is running and I am coding, this was how I found the bug by the way).一切似乎都工作正常,直到我从浏览器刷新页面(或者它在 ng serve 运行并且我正在编码时自行刷新,顺便说一句,这就是我发现错误的方式)。

On refreshing the page the data fetched from the API no longer shows.刷新页面时,从 API 获取的数据不再显示。 It works when navigating from a different page.它在从不同页面导航时起作用。

I am using Django rest framework at the backend to serve API to angular client.我在后端使用 Django rest 框架为 API 提供给 ZD18B8624A0F5F721DADD7B82 客户端。 My app authenticates the user in the backend using JWT and in the frontend uses firebase.我的应用在后端使用 JWT 对用户进行身份验证,在前端使用 firebase。

I am using Http interceptors to add the JWT token to all the Http request.我正在使用 Http 拦截器将 JWT 令牌添加到所有 Http 请求中。

auth.service.ts to get token auth.service.ts 获取令牌

getToken() {
   let tokenPromise = new Promise ((resolve, reject) => {
    this.afAuth.auth.onAuthStateChanged( user => {

      if (user) {

        this.afAuth.auth.currentUser.getIdToken()
         .then(token => {
           this.userToken = token;
         });
      }

      if (this.userToken) {
        resolve(this.userToken);
      }

    });
   });

   return tokenPromise;

  }

api.service.ts to send http request api.service.ts 发送 http 请求

public getEmployees(): Observable<Employee[]> {
    return this.http.get<Employee[]>(`${this.url}/employee/`);
  }

token-interceptor.service.ts for adding the token token-interceptor.service.ts 用于添加令牌

@Injectable({
  providedIn: 'root'
})
export class TokenInterceptorService implements HttpInterceptor {


  constructor(private authService: AuthService) { }

  intercept(req, next): Observable<any> {

    return from(this.authService.getToken()).pipe(
      switchMap(token => {
          const headers = req.headers
              .set('Authorization', 'JWT ' + token)
              .append('Content-Type', 'application/json');
          const reqClone = req.clone({
              headers
          });
          return next.handle(reqClone);
      }));
 }
}

On Chrome browser developer tool's network tab I can see that employee/ is fetched on normal navigation but when the browser is refreshed, employee/ is no longer there.在 Chrome 浏览器开发工具的网络选项卡上,我可以看到在正常导航中获取了 employee/,但是当刷新浏览器时,employee/ 不再存在。

The problem was with the following line: this.afAuth.auth.currentUser.getIdToken()问题出在以下行:this.afAuth.auth.currentUser.getIdToken()

currenUser can be null and we need to afAuth.authState currenUser 可以是 null 我们需要 afAuth.authState

The detailed answer can be found on the following link:详细答案可以在以下链接中找到:

How to fix "TypeError: Cannot read property 'getIdToken' of null" on browser refresh, in angular firebase? 如何在 angular firebase 中修复“TypeError: Cannot read property 'getIdToken' of null”?

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Java / Angular:键值管道不显示从后端发送的数据 - Java / Angular: keyvalue pipe doesn't show data sent from backend 如何以角度显示从后端 api 接收到的数据 - how can i show the data received from backend api in angular 从API访问实时数据(访问Dynamo DB数据),而无需(异步)刷新Angular 6中的页面 - Access Real-time Data from an API (accessing Dynamo DB data) without refreshing the page (asynchronously) in Angular 6 从 json api angular 获取对象中的对象不显示 - Getting object in object from json api angular doesn't show 错误不显示数据库中的数据(spring boot + angular 8) - Error doesn't show data from database (spring boot + angular 8) Angular2:刷新子组件上的页面后无法从服务获取数据 - Angular2: Can't get data from service after refreshing page on child component 如何以角度设置图表的数据,该数据取自存储在 mongodb 中并显示在 html 页面上的后端 nodejs api - How to set the data of chart in angular which is taken from the backend nodejs api that is store in mongodb and showing on the html page Angular Frontend Docker 容器不会从 node express 后端 Docker 容器发送/接收数据 - Angular Frontend Docker container doesn't send/receive data from node express backend Docker container Angular:Observable 服务无法从 api 获取数据 - Angular: Observable service doesn't get data from api 为什么在Angular 2中无法从Web API映射数据 - why doesn't mapping data from web API work in Angular 2
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM