retrievePosts(userId?: string): Observable<any> {
const params = userId ? new HttpParams().set('userId', userId.toString()) : null;
return this.http.get(ApiUrl + ApiPath, { params });
}
I am attempting to streamline the two http.get
calls by avoiding the use of if and else statements. Essentially, if a userId is provided, I want to create a query parameter and include it in the API request, otherwise, I do not want to include any parameters.
I experimented with using a ternary operation but encountered an issue:
this.http.get(ApiUrl + ApiPath, userId ? { params } : null);
Upon inspecting the RequestUrl in the browser's inspector, I noticed the request appeared as follows: "api/Post?userId="