My app allowed user to define backend fields (multiple), then using the 'rules' to chain them (by looping them) to return relevant docs inside a collection.
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)
})
But in the actual fact, the eeDoc variable only can recognize the LAST criteria in the loop and not to chain all criteria up. Example if my rules are
I am expecting it to return all Finance Staff who are Fulltimer, but my code will only return 'Finance' Staff How to fix my code to chain the .where criteria?
You're overwriting the eeDoc
in every iteration of the loop.
This should be closer to what you want:
let eeDoc: any = eeRef
if (u1.bAccessAll) {
for (let r of u.rules) {
eeDoc = eeDoc.where(r.field, r.condition, r.value)
}
}