(Angularfire2 + Ionic3) Recover value from a Child for a variable

Asked

Viewed 202 times

0

Good morning guys, I am new to the Ionic/Angularfire branch and I am facing a great difficulty to recover the value of a Child 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]

This is my attempt to recover Child’s "typeuser value"

  • Avoid displaying your code through images.

  • Thanks for the tip Leandro! It was my first post, I did not know the tools that the site has to publish the code! Now I know!

3 answers

0

try like this:

this.db.object('clientes/'+this.id)
.valueChanges()
.subscribe(statususer => {
    var statusCliente = statususer.status;
});
  • thanks for the help, I managed to get to the value with the code you gave me, I just needed to implement the console.log. But still gave an error there, I added as an answer, I wonder if you could help me?

  • but what was the error? print the error screen if possible

  • I’ve added it there as an answer now.

  • what is the value of this.userid? checks this by trying something like this: console.log('Users/'+this.userid+'/Personal Data/typeuser');

  • I did it. I’ll post the solution I found in the answers! Thanks there for the strength Ronaldo Freitas! Vlw msm!

0

When manually assigning 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 can receive the Child value "typeuser" and store it in a variable. inserir a descrição da imagem aqui

But when I put the userid variable (as described below) I get "null" as 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;
      });
  }

0

After trying several times 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 managed to solve leaving only the function getIdUser inside the constructor and inside 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)
      }
    }

Browser other questions tagged

You are not signed in. Login or sign up in order to post.