【问题标题】:Managing multiple where conditions with Firestore使用 Firestore 管理多个 where 条件
【发布时间】:2020-07-17 17:54:13
【问题描述】:

我正在使用 3 个搜索字段从我的 collection 获取结果。

如果this.tag 为空,我不希望此.where("tagarray", "array-contains", this.tag) 工作,同样如果this.dt 为空.where('lastActivityDate', '>=', this.dt) 不应该工作。 this.leadstage 也一样。

我该如何处理这种情况?

  loadItems(){
    this.firestore.collection('leads' , ref => ref 
.where("tagarray", "array-contains", this.tag) 
.where('lastActivityDate', '>=', this.dt) 
.where("leadstage" , "==" , this.leadstage) 

编辑 1

我这个函数的全部代码

loadItems(){
this.firestore.collection('leads' , ref => ref 
.where("tagarray", "array-contains", "tag1") //tag success
.where('lastActivityDate', '>=', '07-APR-2020') //date success
.where("leadstage" , "==" , 'Prospect') // leadstage success, this is case sensitive
.limit(5)
).snapshotChanges()
.subscribe(response => {
if(!response.length){
  console.log("no data available");
  return false;
}
this.tableData = [];
for(let item of response){
  var data = Object.assign(item.payload.doc.data(), {id : item.payload.doc.id})
  this.tableData.push(data);

...

编辑 2

错误截图

编辑 3

当前代码

    this.firestore.collection('leads' , (ref) => {
      let query = ref;
      if (this.tag) {
        query = query.where("tagarray", "array-contains", this.tag) 
      }
      if (this.dt) {
        query = query.where('lastActivityDate', '>=', this.dt) 
      }
      if (this.leadstage) {
        query = query.where("leadstage" , "==" , this.leadstage) 
      }
      return query; 
    }
    )
    .limit(5)
.snapshotChanges()
.subscribe(response => {
if(!response.length){
  console.log("no data available");
  return false;
}
...

错误:

类型“查询”缺少以下属性 键入“CollectionReference”:id、父级、路径、文档、 添加(2739)

类型上不存在属性“限制” 'AngularFirestoreCollection'.ts(2339)

【问题讨论】:

  • 我的代码和那个不一样,有人在帮助我,请不要关闭问题
  • 您可以编辑您的问题,在末尾添加 Edit 3,以解释为什么它不是重复的。详细一点,然后 ping @DougStevenson 说你已经更新了问题。
  • 该线程中给出的答案是使用 get() 方法,我已经实现了它,但我正在使用 ('leads' , ref => ref ....snapshotChanges() 并且根据文档,事情不起作用,@frank 也在尝试帮帮我,但到现在还没有解决。

标签: angular typescript firebase google-cloud-firestore angularfire2


【解决方案1】:

您可以使用多个条件检查来构建查询,如下所示:

this.firestore.collection('leads' , (ref) => {
  let query = ref;
  if (this.tag) {
    query = query.where("tagarray", "array-contains", this.tag) 
  }
  if (this.dt) {
    query = query.where('lastActivityDate', '>=', this.dt) 
  }
  if (this.leadstage) {
    query = query.where("leadstage" , "==" , this.leadstage) 
  }
  return query; 
})...

【讨论】:

  • loadItems(){ let query = ref; if (this.tag) {.... 找不到名称参考
  • 我已经编辑了问题,复制了整个函数,如果你可以看一下并相应地指导
  • 我的答案中的ref 与您在ref => ref .where... 中的问题中使用的相同。
  • 在我的代码中是 ('leads' , ref => ref) ,当我输入你所引导的内容时,它说找不到名称 ref,我已经复制了我的整个函数,如果你可以看看并更新你的答案,会很有帮助
  • 我更新了我的问题,添加了截图,见Edit:: 2
猜你喜欢
  • 2018-06-10
  • 1970-01-01
  • 1970-01-01
  • 2020-01-22
  • 2013-07-09
  • 2021-04-08
  • 1970-01-01
相关资源
最近更新 更多