0

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

enter image description here

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)

halfer
  • 19,824
  • 17
  • 99
  • 186
user2828442
  • 2,415
  • 7
  • 57
  • 105
  • my code is different than that one, someone is helping me, please dont close the question – user2828442 Apr 05 '20 at 19:24
  • You may edit your question, to add an **Edit 3** at the end, to explain why it is not a duplicate. Be detailed, and then ping @DougStevenson to say you have updated the question. – halfer Apr 05 '20 at 21:13
  • The answer given in that thread is using `get()` method , i have already achieved it with that but i am using `('leads' , ref => ref ....snapshotChanges()` and as per docs things are not working, @frank is also trying to help me, but till now it is not resolved. – user2828442 Apr 06 '20 at 04:49

1 Answers1

1

You can build your query with multiple condition checks, like this:

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; 
})...
Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807