(AngularFire2 + Ionic3) Retrieving value from a child to a variable

0

Good morning, I'm new to the Ionic / AngularFire branch and I'm having a hard time recovering the value of a child from within my Database. The idea is to take this value and store it in a variable so I can make some comparisons within my code.

This is my Database ]

Thisismyattempttorecoverthevalueofchild"typeuser"

    
asked by anonymous 25.04.2018 / 14:24

3 answers

0

try this:

this.db.object('clientes/'+this.id)
.valueChanges()
.subscribe(statususer => {
    var statusCliente = statususer.status;
});
    
25.04.2018 / 14:40
0

When I manually assign the userId within the path

this.afData.object('Usuários/pqMaisUMrWUrk8v8TZmvzQjd1H82/Dados Pessoais/typeuser')

Full function:

constructor(
public navCtrl: NavController,
private afData: AngularFireDatabase,
private afAuth: AngularFireAuth) {

  this.afAuth.authState.subscribe(user => {
    if(user) this.userId = user.uid})
  this.afData.object('Usuários/pqMaisUMrWUrk8v8TZmvzQjd1H82/Dados Pessoais/typeuser')
      .valueChanges()
      .subscribe(dataclient=>{
        console.log(dataclient)
        this.typeUser=dataclient;
      });
  }

I get the value of the child "typeuser" and store it in a variable.

ButwhenIputthevariableuserId(asdescribedbelow)Iget"null" as a return.

this.afData.object('Usuários/'+this.userId+'/Dados Pessoais/typeuser')

Full function:

  constructor(
public navCtrl: NavController,
private afData: AngularFireDatabase,
private afAuth: AngularFireAuth) {

  this.afAuth.authState.subscribe(user => {
    if(user) this.userId = user.uid})
  this.afData.object('Usuários/'+this.userId+'/Dados Pessoais/typeuser')
      .valueChanges()
      .subscribe(dataclient=>{
        console.log(dataclient)
        this.typeUser=dataclient;
      });
  }

    
25.04.2018 / 15:13
0

After trying several times performing checks to verify the values of the variables in the HTML page I discovered that the values were there in the variables but were not being passed to the other functions. I was able to solve by leaving only the getIdUser function inside the constructor and inside it calling the others and it worked!

constructor(
public navCtrl: NavController,
private afData: AngularFireDatabase,
private afAuth: AngularFireAuth) {
  this.afAuth.authState.subscribe(user => {
    if(user) this.userId = user.uid
    this.searchTypeUser()});
    }

    searchTypeUser(){
      this.afData.object('Usuários/'+this.userId+'/Dados Pessoais/typeuser')
      .valueChanges()
      .subscribe(dataclient=>{
        console.log(dataclient)
        this.typeUser=dataclient
        this.redirect()});
    }

    redirect(){
      if (this.typeUser=='client') {
        this.navCtrl.push(ClientPage)
      }if(this.typeUser=='coordinator'){
        this.navCtrl.push(CoordinatorPage)        
      }if(this.typeUser=='promoter'){
        this.navCtrl.push(PromoterPage)
      }if(this.typeUser=='admin'){
        this.navCtrl.push(ConsolePage)
      }
    }
    
25.04.2018 / 17:23