Ad

Query Single Value From Firebase, React Native-Error, Undefined

I am trying to query a value from a document in collection, in fire base, particularly "currentKilos":

fire base collection

I am using the below function to query it:

showKiols = () => {
  db.collection('users').doc(this.props.user.uid).collection('mainData').doc('currentKilos').get().then.subscribe(value=> {
        this.updatedKilos = value;
        alert(this.updatedKilos);           
    })      
}

However, I am getting attached error:

error

Also, I tried below function as well, but it is not working:

db.collection('users').doc(this.props.user.uid).collection('mainData').doc('currentKilos').valueChanges().subscribe(value => {
this.updatedKilos = value.currentKilos;
    alert(this.updatedKilos)
})

FYI, this is the function to add currentKilos to firebase, and it is working fine:

updateKilos = () => {
        db.collection('users').doc(this.props.user.uid).collection('mainData').doc('currentKilos').set({ currentKilos: this.state.currentKilos })
        alert('Data Updated')
    }

Appreciate your support.

Ad

Answer

I'm not sure where you get get().then.subscribe() from, but at the very least that then should be then((value)=>{...}).

Try the following:

showKiols = () => {
db.collection('users')
 .doc(this.props.user.uid)
 .collection('mainData')
 .doc('currentKilos')
 .get()
 .then(value => {
     this.updatedKilos = value.data();
     alert(this.updatedKilos);           
 })
}
Ad
source: stackoverflow.com
Ad