Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

271
Views
Angular - getting params from route.url sometimes results in "undefined" text

I have a question.

I found a weird bug that I can't reproduce on my machine. My app has a flow like this:

  1. User clicks a short url, calls backend (nodejs) and the backend will do a redir to angular frontend. For example to: (https://mysite/#/actpost/param1/param2)
  2. Every time the backend does a redir, I put it in log.txt, I have checked and they all seem to be correct, redirecting to the correct frontend urls (no "undefined string" in the url)
  3. In frontend, it will do url split to get the params and post a request to backend with said params, I also log it in log.txt during every post call (in backend side) The code to get params is as follows :
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!

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

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)}});
  }
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!