为什么firestore where子句不适用于状态? [英] Why is firestore where clause not working with state?

查看:36
本文介绍了为什么firestore where子句不适用于状态?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用react js中的where子句从firestore中过滤文档.但是,当我在where子句中使用状态变量时,它不起作用,但是当我提供实际字符串时,它就开始起作用

I am trying to filter documents from firestore using where clause in react js. However when i use a state variable in the where clause it is not working but when i provide an actual string it starts working

不起作用

firebase.firestore().collection('Partners')
    .where("Email",'==',this.state.currentUser).get().then( querySnapshot => {
      querySnapshot.forEach(doc => {

        this.setState({
          Theatre:doc.data().Theatre
        })

    })
})

工作

firebase.firestore().collection('Partners')
    .where("Email",'==',"test@test.com").get().then( querySnapshot => {
      querySnapshot.forEach(doc => {

        this.setState({
          Theatre:doc.data().Theatre
        })

    })
})

任何帮助将不胜感激.谢谢.

Any help would be appreciated. Thank you.

编辑我什至尝试了 console.log(this.state.currentUser ==='test@test.com'),它实现了.但是where子句不适用于状态变量

EDIT I even tried console.log(this.state.currentUser==='test@test.com') and it is coming true. Yet the where clause is not working with the state variable

推荐答案

反应状态是异步的,但您确实会在控制台中获得状态值,但是在运行Firestore查询时,因为它也是异步的,您的状态还是以某种方式被覆盖或也许您的页面正在刷新,并且状态已重新设置.

React state is asynchronous you do get a state value in a console but by the time firestore query is ran because it is also asynchronous your state is somehow getting override or maybe your the page is getting refreshed and state is being set again.

我遇到了同样的问题,然后我检查了页面的刷新情况,即使我将其保存在console.log()中,我也失去了设置的状态,但是到了我运行查询的Firestore时,却没有状态.

I had this same issue and then I checked my page was getting refreshed and I was losing the state I set even though I did got it inside the console.log() but by the time my firestore where query ran there was not state.

您需要在此处提供完整的代码,以便我们查看.

You need to provide the whole code here in order for us to take a look at it.

这篇关于为什么firestore where子句不适用于状态?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆