简体   繁体   English

Firestore + React.js 如何通过用户过滤从 collections 获取文档?

[英]Firestore + React.js how to get documents from a collections by filtering from user?

I have three fields for data, by default in the constructor they all setted to all.我有三个数据字段,默认情况下在构造函数中它们都设置为全部。 What ı want to do is making multiple queries on these fields like in mysql.我想做的是对这些字段进行多个查询,例如 mysql。 For example: where field1 is apple, field2 is orange and ı do not touch the field 3.(Tumu means "All" by the way.) Here is my code:例如:field1 是苹果,field2 是橙色,我不要触摸字段 3。(顺便说一下,Tumu 的意思是“全部”。)这是我的代码:

class Kurumlar extends Component {
constructor(props) {
    super(props);
    this.state = {
        data: [],
        isLoading: true,
        page: 1,
        HizmetTuru: "Tumu",
        HizmetAlani: "Tumu",
        Ucretlendirme: "Tumu"
    };
}

handleChange = e => {
    this.setState({
        [e.target.name]: e.target.value
    }, console.log(this.state));
}


componentDidMount() {
    const db = firebase.firestore();
    let docs = db.collection("Paydaslar")

    console.log(this.state)

    //docs = db.collection("Paydaslar").where("HizmetTuru", "==", this.state.HizmetTuru);
    docs.get()
        .then(snapshot => {
            let data = [];
            snapshot.forEach(doc => {
                if (doc.data().isVisible) {
                    data.push(doc.data());
                }
            });

            this.setState({ data, isLoading: false });
        })
        .catch(err => {
            console.log("Error getting documents", err);
        });
}

componentDidUpdate(){
    const db = firebase.firestore();
    let docs = db.collection("Paydaslar")

   if(this.state.HizmetTuru != "Tumu"){docs=db.collection("Paydaslar").where("HizmetTuru", "==", this.state.HizmetTuru)}
   if(this.state.HizmetAlani != "Tumu"){docs=db.collection("Paydaslar").where("HizmetAlani", "==", this.state.HizmetAlani)}
  if(this.state.Ucretlendirme != "Tumu"){docs=db.collection("Paydaslar").where("Ucretlendirme", "==", this.state.Ucretlendirme)}


    docs.get()
        .then(snapshot => {
            let data = [];
            snapshot.forEach(doc => {
                if (doc.data().isVisible) {
                    data.push(doc.data());
                }
            });

            this.setState({ data, isLoading: false });
        })
        .catch(err => {
            console.log("Error getting documents", err);
        });
}

By this way if 1 field is setted to a value(different from all(Tumu in the code), other fields do not work.通过这种方式,如果将 1 个字段设置为一个值(与 all(代码中的 Tumu)不同),其他字段将不起作用。

If I understand correct you want to build a query with multiple possible conditions, depending on what the user selected.如果我理解正确,您希望根据用户选择的内容构建具有多种可能条件的查询。

In that case you can use the builder pattern:在这种情况下,您可以使用构建器模式:

const db = firebase.firestore();
let query = db.collection("Paydaslar").collection("Paydaslar")

if(this.state.HizmetTuru != "Tumu") {
  query = query.where("HizmetTuru", "==", this.state.HizmetTuru)
}
if(this.state.HizmetAlani != "Tumu") {
  query = query.where("HizmetAlani", "==", this.state.HizmetAlani)
}
if(this.state.Ucretlendirme != "Tumu"){
  query = query.where("Ucretlendirme", "==", this.state.Ucretlendirme)
}

docs.get()
  ...

I figured it out just in the if parts, do:我只是在 if 部分发现了它,请执行以下操作:

    if (this.state.HizmetTuru != "Tumu") { docs = docs.where("HizmetTuru", "==", this.state.HizmetTuru) }
    if (this.state.HizmetAlani != "Tumu") { docs = docs.where("HizmetAlani", "==", this.state.HizmetAlani) }
    if (this.state.Ucretlendirme != "Tumu") { docs = docs.where("Ucretlendirme", "==", this.state.Ucretlendirme) }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM