I am fetching results from my collection
using 3 search fields.
If this.tag
is empty I don't want this .where("tagarray", "array-contains", this.tag)
to work, similarly if this.dt
is empty .where('lastActivityDate', '>=', this.dt)
should not work. Same for this.leadstage
.
How do I manage this condition ?
loadItems(){
this.firestore.collection('leads' , ref => ref
.where("tagarray", "array-contains", this.tag)
.where('lastActivityDate', '>=', this.dt)
.where("leadstage" , "==" , this.leadstage)
Edit 1
My entire code of this function
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);
...
Edit 2
Screenshot of error
Edit 3
Current code
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;
}
...
Errors:
Type 'Query' is missing the following properties from type 'CollectionReference': id, parent, path, doc, addts(2739)
Property 'limit' does not exist on type 'AngularFirestoreCollection'.ts(2339)