user9520310
user9520310

Reputation:

using the setter method ["set()"] under HttpParams

I need to convert the following method to Angular 7. The data doesn't set to the params. can someone point me out why doesn't it works? It says

error TS2300: Duplicate identifier 'params'.

updateParamsWithAuth(params: HttpParams, appKey: string, field: string) {
let token;
if (appKey && appKey.length > 0) {
  token = this.getAccessTokenFromSession();

} else {
  token = this.getRSTokenFromSession();
  if (field === this.FIELD_ACCESS_TOKEN) {
    field = this.FIELD_RS_TOKEN;
  }
}

// params.set(field, token);
let params = new HttpParams().set(field, token);

return params;
}

Upvotes: 2

Views: 83

Answers (2)

Sadid Khan
Sadid Khan

Reputation: 1985

params is a parameter of your method and you declared another variable named params before return and that is the issue.

cahnge this let params = new HttpParams().set(field, token);

to this params = new HttpParams().set(field, token);

But my question is why you need to reinitialize it here? If you initialize it here again there is no point to pass it as a parameter of that method, isn't it?

You already have params inside the method, so, I think no need to initialize it again.

If you don't want the value of params that comes to you as a parameter better remove that parameter from that method as you are initializing a new HttpParams inside the method. Or else use the existing params.

From my point the code should be like this -

updateParamsWithAuth(params: HttpParams, appKey: string, field: string) {
    let token;
    if (appKey && appKey.length > 0) {
      token = this.getAccessTokenFromSession();

    } else {
      token = this.getRSTokenFromSession();
      if (field === this.FIELD_ACCESS_TOKEN) {
        field = this.FIELD_RS_TOKEN;
      }
    }

    params = params.set(field, token);

    return params;
}

Upvotes: 0

Sudarshana Dayananda
Sudarshana Dayananda

Reputation: 5265

You are passing params as a parameter to updateParamsWithAuth function and again inside function declaring params variable again. Just use,

params = new HttpParams().set(field, token);

or declare it with deferent identifier.

let httpParams = new HttpParams().set(field, token);

Upvotes: 1

Related Questions