I have a question.
I found a weird bug that I can't reproduce on my machine. My app has a flow like this:
ngOnInit(): void {
this.param1= this.router.url.split('/')[2];
this.param2 = this.router.url.split('/')[3];
//Do a POST request to backend
apiCall(param1, param2);
I then check the post log and this is where I found the bug, sometimes param1 in the request body contains the string "undefined" rather than the actual string from in the url (param1).
Which leads me to this question: is the router function async? I have also filled both param1 & param2 with empty string before trying to get params from URL, so I don't know how it can get filled with "undefined" string. I have also tried getting params via activated Route paramMap and the same problem still happens.
Thanks!
The router is asynchronous, below a code example for getting URL parameters.
private ngOnInit(): void {
this.route.queryParams.pipe(
map(params => [params['id1'],params['id2']]),
tap(([a1,a2]) => {console.log("A1:",a1,"A2:",a2)}),
catchError((err) => {
return throwError(()=>new Error(`Error : ` + err))
})
).subscribe({next: ([a1,a2])=>{apiCall(a1,a2)}});
}