TypeError [ERR_INVALID_ARG_TYPE]: The "url" argument must be of type string. Received undefined (ANGULAR 14.2.1)

Viewed 28

I have an SSR angular app built with V14.2.1 In my gateway, I have a method that returns a GET request.

const url = environment.apiUrl;

@Injectable()
export class SampleGateway {

  constructor(private httpClient: HttpClient){}

  getData(id:number): Observable<SomeInterface> {

  let params: HttpParams = new HttpParams();
  params = params.set('id',id);

  return this.httpClient.get<SomeInterface>(`${url}/entity`, {params: params});

  }

}

The app builds successfully on the server-side but when this method is called on one of the pages I get this TypeError [ERR_INVALID_ARG_TYPE]: The "url" argument must be of type string. Received undefined (ANGULAR 14.2.1) error and the page doesn't load.

If I remove options from the request it works correctly.

return this.httpClient.get<SomeInterface>(`${url}/entity`);

What the hell is going on?

1 Answers
Dont forget import HttpParams and if you try to set() to params you must define new variable too. 

import { HttpClient, HttpParams } from '@angular/common/http';

getParams(id): Observable<any> {

    let params = new HttpParams().set('id', id);

    return this._HttpClient.get(`${url}/entity`, { params: params })
}

Related