Przemysław Zamorski
Przemysław Zamorski

Reputation: 741

Angular 4 cant pass null as parameter

In my program i want to pass get with parameters. One of those parameters are null. But when i set this property to URLSearchParams it doesnt appers in final Url? Is this possible?

Here the object with parameters which i pass as parameters:

{zpm_id:112, tow_id:null}

And here part of my function:

public getDaneOkna(rodzaj: string, symbolOkna: string, parametry: any) {  
 let params = new URLSearchParams();
 Myheaders = new Headers();
 for (var key in parametry) {
      params.set(key, parametry[key]); 
    }

 return this._http.get(this.adresSerwera + '/dane/' + symbolOkna,
      {
        headers: Myheaders,
        search: params
      }
    )

Upvotes: 0

Views: 6068

Answers (2)

diopside
diopside

Reputation: 3062

Its generally suggested to avoid using null completely in typescript. Use 'undefined' instead.

https://basarat.gitbooks.io/typescript/docs/tips/null.html

But if for some reason you ended up passing parametry as undefined, you won't be able to access any properties of it, so this

 parametry[key]

would generate an error, whereas this

 console.log(parametry)

Wouldn't generate an error, it would just print 'undefined'

Other commenters are right its best to avoid passing in an undefined parameter. If you must, checks inside the method for its existence would be wise...

Upvotes: 0

Carsten
Carsten

Reputation: 4208

If the value == null, don't add it with params.set...

Upvotes: 1

Related Questions