简体   繁体   English

Angular V6 Pipe(map())问题

[英]Angular v6 pipe(map()) issue

I would appreciate if someone could shed some light on this. 如果有人可以对此有所阐明,我将不胜感激。 I have been at it for days now. 我已经去了好几天了。

Here is two functions that lives in my auth service. 这是我的身份验证服务中存在的两个函数。 The login function that retrieves a valid jwt and then the refresh function that fetches a refreshed jwt. 登录函数先检索有效的jwt,然后再刷新函数以获取刷新的jwt。

LOGIN 登录

  login(username: string, password: string): Observable<any> {
    const headers = new HttpHeaders().set('Authorization', `Basic ${environment.WSO2_AUTH_BASIC}`);
    const params = new HttpParams({
    fromObject: {
     grant_type: 'password',
     scope: 'openid',
     username: username,
     password: password
   }
 });

  return this.http.request<Token>('POST', environment.API_HOST + '/token', {
    headers: headers,
    body: params
  }).pipe(map(this._mapTokenResponse));
}

REFRESH 刷新

  private _refreshToken() {
const headers = new HttpHeaders().set('Authorization', `Basic ${environment.WSO2_AUTH_BASIC}`);
this.token = this.getToken();
const params = new HttpParams({
  fromObject: {
    grant_type: 'refresh_token',
    scope: 'openid',
    refresh_token: this.token.refresh_token
  }
});
return this.http.request<Token>('POST', environment.API_HOST + '/token', {
  headers: headers,
  params: params
}).pipe(map(this._mapTokenResponse, this));
}

I have created a separate arrow function that handles the mapping for both. 我创建了一个单独的箭头函数来处理两者的映射。

private _mapTokenResponse = (token: Token) => {
// login successful if there's a jwt token in the response
if (token && token.access_token) {
  // store user details and jwt token in local storage to keep user logged in between page refreshes
  token.id_token_data = this.jwtHelper.decodeToken(token.id_token);
  this._setSession(token);
}
return token;

} }

I want this so that I do not duplicate code. 我想要这样做,以便不重复代码。 The login function works perfectly but the refresh token returns this error: 登录功能运行正常,但刷新令牌返回此错误:

ERROR Error: "Uncaught (in promise): TypeError: argument is not a function. Are you looking for `mapTo()`?

I have imported map from 'rxjs/operators' 我已经从'rxjs / operators'导入了地图

Either you can do: 您可以执行以下操作:

  return this.http.request<Token>('POST', environment.API_HOST + '/token', {
    headers: headers,
    body: params
  }).pipe(
    map(this._mapTokenResponse.bind(this))
  );

We use .bind(this) to set up the scope (the "this") of the function call. 我们使用.bind(this)设置函数调用的范围(“ this”)。 Otherwhise, you'll get an error each time you call this. 否则,每次调用都会出错this. in your callback function. 在您的回调函数中。

Or: 要么:

  return this.http.request<Token>('POST', environment.API_HOST + '/token', {
    headers: headers,
    body: params
  }).pipe(
    map((token: Token) => this._mapTokenResponse(token))
  );

This solution is much cleaner in my opinion. 我认为这种解决方案更加干净。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM