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

117
Visualizações
JS subscribe not working on first attempt

I have some TS code that checks the nearest point around you for car assistance (determined by a database), and after having establishhed that he takes its phone number and calls it. Problem: it only works after the first click that triggers the event, first attempt returning undefined as a result.

 repairerInfoCall: RepairerInfo;
 userLongitude: number;
 userLatitude: number;
 repairersHeaderInfo: RepairerHeaderInfo[]; /established onInit/
        navigateToComponent(navigationButton: NavigationButtonsEnum) {
            switch (navigationButton) {
               /.../
               case NavigationButtonsEnum.RoadsideAssistance:
                /.../
                else if (this.userLatitude != null && this.userLongitude != null && this.repairersHeaderInfo != null) {
                  let data = this.repairersHeaderInfo.reduce((prev, current) => (prev.distance < current.distance) ? prev : current);
                  this.repairerService.getCompanyInfoById(data.id.toString()).subscribe(repInfo =>{
                    this.repairerInfoCall = repInfo
                  })
                  window.location.href = "tel:" + this.repairerInfoCall.phoneNumber;
                } 
        /.../

HTML:

<button mat-raised-button class="home-buttons"
      (click)="navigateToComponent(navigationButtonsEnum.RoadsideAssistance)">
      <img src="assets/icons/tow-truck.png" class="home-icons">
      <div>Soccorso stradale</div>
    </button>
  </div>

The code works and it rightly gets all the info needed (from server). The only issue is that the first time it tries to subscribe it doesn't, meanwhile the second time and later it works pefectly. Why is that?

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

The Problem here is that the local repairerInfoCall won't be updated before calling window.location.href = "tel:" + this.repairerInfoCall.phoneNumber;

Observables extends Promises, they are asynchronous.

You could do following:

async navigateToComponent(navigationButton: NavigationButtonsEnum) {
 /.../
 this.repairerInfoCall = await  this.repairerService.getCompanyInfoById(data.id.toString()).toPromise();
 window.location.href = "tel:" + this.repairerInfoCall.phoneNumber;
 /.../
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