【问题标题】:How to chain .where for firebase query如何链接.where进行firebase查询
【发布时间】:2021-05-16 02:36:55
【问题描述】:

我的应用允许用户定义后端字段(多个),然后使用“规则”链接它们(通过循环它们)以在集合中返回相关文档。

 let eeRef = this.afStore.collection<any>(`.../Collections/EmpSnapshot`).ref
 let eeDoc: any = null
 if (u.bAccessAll) {
     eeDoc = eeRef
 } else {
    for (let r of u.rules) {
        eeDoc = eeRef.where(r.field, r.condition, r.value)
    }
 }

await eeDoc.get()
.then(res => {
    for (let ee of res.docs) {
        emails.push(ee.id)
    }
}).catch(err => {
    console.log(`Error query-ing 'EmpSnapshot': `, err)
})

但实际上,eeDoc 变量只能识别循环中的 LAST 条件,而不能将所有条件链接起来。例如,如果我的规则是

  • 全职(员工)
  • 财务(部门)

我希望它返回 所有全职财务人员,但我的代码只会返回 'Finance' Staff 如何修复我的代码以链接 .where 条件?

【问题讨论】:

    标签: javascript firebase google-cloud-firestore angularfire2


    【解决方案1】:

    您在循环的每次迭代中都覆盖了eeDoc

    这应该更接近你想要的:

     let eeDoc: any = eeRef
    
     if (u1.bAccessAll) {
        for (let r of u.rules) {
            eeDoc = eeDoc.where(r.field, r.condition, r.value)
        }
     }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-11-17
      • 1970-01-01
      • 1970-01-01
      • 2016-02-08
      • 2021-06-17
      • 1970-01-01
      • 2015-02-10
      相关资源
      最近更新 更多