Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

169
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda