Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Angular 5 HttpParams not being set

I have thins very simple function:

createParams(paramsArray, withToken: boolean): HttpParams {
    let params = new HttpParams();
    let currentUser = JSON.parse(localStorage.getItem('currentUser'));
    params.set('access_token', JSON.stringify(currentUser.token));
    return params;
}

When i debug this the params variable does not contain any keys nor values:

enter image description here

What am i doing wrong?

like image 536
Marc Rasmussen Avatar asked May 23 '18 07:05

Marc Rasmussen


1 Answers

Try this:

let Params = new HttpParams();
Params = Params.append('access_token', JSON.stringify(currentUser.token));

OR

let params = new HttpParams().set('access_token', JSON.stringify(currentUser.token)); 

HttpParams is intended to be immutable. The set and append methods don't modify the existing instance. Instead they return new instances.

like image 147
Hasan Fathi Avatar answered Sep 29 '22 20:09

Hasan Fathi