FennyChen
FennyChen

Reputation: 31

Angular2 activatedRoute queryParam get undefined param from subscribe

In my component, in ngOnInit, I have a subscribe to activedRoute.queryParam to get query parameters. But sometimes it gets undefined values which break the subsequent API calls since the undefined gets passed into the API.

    this.activatedRoute.queryParams.subscribe(params => {     
        this.personId = params['personId'];
    });

How should I make sure I get the personId first before I make API calls. Any help would be highly appreciated.

Upvotes: 0

Views: 781

Answers (1)

callback
callback

Reputation: 4122

You can use the filter operator as follows to make sure you subscribe only when the value exists:

this.activatedRoute.queryParams.filter(params => params['personId'])
.subscribe(params => {     
   this.personId = params['personId'];
   // make your other API calls..   

});

Upvotes: 2

Related Questions