1

I have written a method in Angular, and I executed it by passing the argument q='status'. However, the first console.log(params) did not include q='status', whereas the second console.log(params) included it. Why is that?

    getHospitalProfile(urlName: string, q: string | null = null): Observable<any> {
        let params = new HttpParams();
        if (q !== null) {
            params = params.set('q', q);
            console.log(params);
        }
        params = params.set('q', 'status');
        console.log(params);
        return this.http.get(`${this.apiUrl}/hospitals/${urlName}`, {
        ...this.httpOptions,
        observe: 'response',
        params: params
        })
        .pipe(
            map((res: HttpResponse<any>) => {
                if (res.status === 200) {
                    return res.body;
                }
            }),
            catchError(error => {
                throw error('ng');
            })
        );
    }
gogogogogo
  • 129
  • 7
  • 2
    You mean, the console.log after `params = params.set('q', q)`? What is the value of `q` here, or in other words, how is `getHospitalProfile` called? – mbojko Apr 05 '23 at 11:23
  • 1
    thanks, I called this method with by `this.apiService.getHospitalProfile(this.urlName, 'status');`. getHospitalProfile is the method of apiService. – gogogogogo Apr 05 '23 at 11:27
  • 1
    It could be due to how console.log presents objects. If you replace `console.log(params)` with `console.log(JSON.stringify(params))`, does it behave as expected? – mbojko Apr 05 '23 at 11:31
  • 1
    `first console.log` {"updates":[{"param":"q","value":"status","op":"s"}],"cloneFrom":{"updates":null,"cloneFrom":null,"encoder":{},"map":null},"encoder":{},"map":null} `second console.log` {"updates":[{"param":"q","value":"status","op":"s"},{"param":"q","value":"status","op":"s"}],"cloneFrom":{"updates":null,"cloneFrom":null,"encoder":{},"map":null},"encoder":{},"map":null} – gogogogogo Apr 05 '23 at 11:37
  • While testing this, I didn't see any problems. Could you create a [minimal reproducible example](https://stackoverflow.com/help/minimal-reproducible-example)? Preferably in an online code editor such as [Stackblitz](https://www.stackblitz.com) – Cuzy Apr 05 '23 at 11:38
  • [This class is immutable; all mutation operations return a new instance.](https://angular.io/api/common/http/HttpParams#description) – traynor Apr 05 '23 at 12:10
  • Does this answer your question? [Angular 4.3 - HttpClient set params](https://stackoverflow.com/questions/45210406/angular-4-3-httpclient-set-params) – traynor Apr 05 '23 at 12:10

2 Answers2

1

Use .append(q). .set() is for replacement.

Ron Strauss
  • 60
  • 1
  • 7
  • thanks, however, according to https://angular.io/api/common/http/HttpParams, both set and append methods appear to return an HttpParams object. – gogogogogo Apr 05 '23 at 11:34
0

I'm sorry. There was no problem with the implementation in question and the issue was caused by something else. Thank you to everyone who provided answers.

gogogogogo
  • 129
  • 7