Home > front end >  How to get one items from my Firebase realtime Database with Angular Ionic
How to get one items from my Firebase realtime Database with Angular Ionic

Time:10-29

this is my Realtime database I want to find the article of the person connected and display infomation

[1]: https://i.stack.imgur.com/HCEga.png*

This is my ionic script page

here are my imports I can retrieve the id of the user to connect but I cannot retrieve this information in the real time database

  import { Component } from '@angular/core';
    import { AngularFireAuth } from '@angular/fire/compat/auth';
    import { AngularFireDatabase, AngularFireList } from '@angular/fire/compat/database';

export class Tab3Page {

  public profileData: any;
  name: string;
prenom: string;



  constructor(private afAuth: AngularFireAuth,
     private db: AngularFireDatabase,
      private toast: ToastController,
      private navCtrl: NavController,
      private authService: FirebaseAuthServiceService) {
  

    this.afAuth.authState.subscribe(
      (res)=>{
        this.profileData = res.uid;
        console.log(this.profileData);      
      }
    )

    this.db.list("users/" this.profileData).valueChanges().subscribe(details => {
      this.name =details["name"];
      this.prenom = details["prenom"];
      console.log(details);
      
    })

}
    
}

CodePudding user response:

It's hard to be certain, but I have a feeling you are attaching the database listener before the user is signed in. To fix that you need to move the subscribe call into the auth state listener"

this.afAuth.authState.subscribe(
  (res)=>{
    this.profileData = res.uid;
    console.log(this.profileData);      
  }
  this.db.list("users/" this.profileData).valueChanges().subscribe(details => {
    this.name =details["name"];
    this.prenom = details["prenom"];
    console.log(details);  
  })
)

If this indeed gets the data loaded you'll want to start managing the database subscription, as the authState may of course change over time.

CodePudding user response:

thank you Frank for your interventions I saw my problem it was just a problem of local variables

this.afAuth.authState.subscribe(
  (res)=>{
    this.profileData = res.uid;
    console.log(this.profileData);

    this.db.list("users/" this.profileData).valueChanges().subscribe(details => {
      this.name = details[2];
      console.log(this.name);
      
    })
    
  }
)
  • Related