Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

273
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda