【问题标题】:Filter Firestore with multiple where not working使用多个不工作的地方过滤 Firestore
【发布时间】:2018-04-30 07:37:36
【问题描述】:

我有这个文档

let wall = firebase.wallCollection;

然后我想用多个 where 过滤它:

  • 位置(位置 == 过滤器位置)
  • 价格(价格

这是我在 Vue 上的过滤方法

filterResult(){
    let self = this;
    if(self.filterLocation!=""){
        wall.where('location','==', self.filterLocation);
        console.log("Location flag");
    }
    if(parseInt(self.filterMaximumPrice)!=0){
        dinding.where('price','<', parseInt(self.filterMaximumPrice));
        console.log("Price flag");
    }

    wall.get()
    .then(snapshots => {
        snapshots.forEach(doc => {
        self.listFilteredWall.push(doc.data());
        }, this);
    })
}

问题是 2 where 功能无法正常工作,并且仍然在没有过滤器的情况下提供所有墙输出。

如何解决这个问题?

【问题讨论】:

  • 大多数情况下,当任何一个值为空时,就会出现此问题。

标签: arrays firebase vue.js google-cloud-firestore


【解决方案1】:

CollectionReference 扩展 Querywhere() 的结果是一个新的Query

创建一个新查询,该查询仅返回包含 指定的字段以及值满足约束的位置 提供。

如果每个where(),您需要保留结果Query,并将其用于get()。像这样的:

filterResult(){
    let self = this;
    let query = wall;

    if(self.filterLocation!=""){
        query = query.where('location','==', self.filterLocation);
        console.log("Location flag");
    }
    if(parseInt(self.filterMaximumPrice)!=0){
        query = query.where('price','<', parseInt(self.filterMaximumPrice));
        console.log("Price flag");
    }

    query.get()
    .then(snapshots => {
        snapshots.forEach(doc => {
        self.listFilteredWall.push(doc.data());
        }, this);
    })
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-01-07
    • 2020-02-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-10-25
    相关资源
    最近更新 更多