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

165
Visualizações
Asynchronous method doesn't work the first time

I'm new in Firebase. I'm using Firestore database and Ionic, I have this problem with an asynchronous call and I can't solve it. Basically in the item variable goes the data that I have saved in the firestore database. But when I want to show them, through a button, in a new html page a strange thing happens, in the url the passed parameter appears and disappears immediately and nothing works anymore. I had a similar problem in the past that I solved using the angular pipe "async" , but in this case it doesn't even work. In detail, I have a list of items in a component:

  ngOnInit() {
    this.itemService.getItemsList().subscribe((res)=>{
      this.Tasks = res.map((t) => {
        return {
          id: t.payload.doc.id,
          ...t.payload.doc.data() as TODO
        };
      })
    });
    
  } 

and in item.service.ts I have defined the function:

constructor(
    private db: AngularFireDatabase,
    private ngFirestore: AngularFirestore,
    private router: Router
) { }

  getItemsList() {
    return this.ngFirestore.collection('items').snapshotChanges();
  }
  getItem(id: string) {
        return this.ngFirestore.collection('items').doc(id).valueChanges();
      }

For each item I have a button to show the detail:

<ion-card *ngFor="let item of Tasks" lines="full">
....
<ion-button routerLinkActive="tab-selected" [routerLink]="['/tabs/item/',item.id]" fill="outline" slot="end">View</ion-button>

In component itemsDescription.ts I have:

  ngOnInit() {
    this.route.params.subscribe(params => {
      this.id = params['id'];
      });
    this.itemService.getItem(this.id).subscribe((data)=>{
    this.item=data;
    });  
  }
 

Finally in html page:

    <ion-card-header>
      <ion-card-title>{{item.id}}</ion-card-title>
    </ion-card-header>
    <ion-icon name="pin" slot="start"></ion-icon>
        <ion-label>{{item.Scadenza.toDate() | date:'dd/MM/yy'}}</ion-label>
    <ion-card-content>{{item.Descrizione}}</ion-card-content>

The Scadenza and Descrizione information are shown, instead id is not. Also the url should be tabs/items/:id but when I click on the button to show the item information, the passed parameter immediately disappears and only tabs/items is displayed. If I remove the data into {{}}, the parameter from the url doesn't disappear

SOLVED I followed this guide https://forum.ionicframework.com/t/async-data-to-child-page-with-ionic5/184197. So putting ? , for example {{item?.id}} now everything works correctly

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

0

Your nested code order not right. You will get the value of id after subscription.

Check this Code:

  ngOnInit() {
    this.route.params.subscribe(params => {
      this.id = params['id'];
 this.profileService.getItem(this.id).subscribe((data)=>{
    this.item=data;
    });  
      });
   
  }
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