【问题标题】:Query single value from firebase, React Native-Error, undefined从firebase查询单个值,React Native-Error,未定义
【发布时间】:2019-07-02 22:51:41
【问题描述】:

我正在尝试从集合中的文档中查询一个值,在火库中,特别是“currentKilos”:

我正在使用下面的函数来查询它:

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

但是,我收到附加错误:

另外,我也尝试了以下功能,但它不起作用:

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

仅供参考,这是将 currentKilos 添加到 firebase 的功能,它工作正常:

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

感谢您的支持。

【问题讨论】:

    标签: javascript firebase react-native google-cloud-firestore


    【解决方案1】:

    我不确定你从哪里得到get().then.subscribe(),但至少then 应该是then((value)=>{...})。

    尝试以下方法:

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

    【讨论】:

    • 非常感谢您的支持。但这给出了“对象对象”的值。当我将函数更改为如下所示: showKiols = () => { db.collection('users').doc(this.props.user.uid).collection('mainData').doc('currentKilos' ).get().then(value=> { this.updatedKilos = value.currentKilos; alert(this.updatedKilos); }) } 它给出 undefined
    • 抱歉,我忘记在返回的value 中包含.data()。立即尝试。
    • 不,需要道歉,非常感谢您的支持。它仍然给出“对象对象”。我什至尝试使用 JSON.stringfy(this.updatedKilos),但效果不佳
    • 我再次尝试,使用以下代码,它正在工作:) showKiols = () => { db.collection('users').doc(this.props.user.uid).collection ('mainData').doc('currentKilos').get().then(value=> { this.updatedKilos = value.data(); alert(JSON.stringify(this.updatedKilos)); }) } 你真的让我开心:)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-12-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-06-04
    • 1970-01-01
    相关资源
    最近更新 更多