Firestore where clause doesn't make the condition.

10,195

Solution 1

You're not chaining the query clauses correctly. Also, you're calling get() in the middle of your chain. That's almost certainly not what you want. Each query object builds on the last, and you should only get() on the final query in the chain:

setApplyStatus() {
  var query = firebase.firestore().collection('applications')
    .where("jobSeekerId", '==', this.jobSeekerId)
    .where("jobId", '==', this.job.id)
    .get().then(querySnapshot => {
      querySnapshot.forEach(doc => {
      console.log(doc.data())
      console.log('already exists')
      this.applyStatus = true
    })
  })
}

Solution 2

For new users, who are watching this in 2021. the code is next: PS. there is no more "==" clause, it's now 'isEqualTo: parameter'

void setApplyStatus() {
    var query = firebase.firestore().collection('applications');
    query = query.where("jobSeekerId", isEqualTo: this.jobSeekerId)
    query = query.where("jobId", isEqualTo: this.job.id)
    query = query.get().then(querySnapshot => {
         querySnapshot.forEach(doc => {
         console.log(doc.data())
         console.log('already exists')
         this.applyStatus = true
         })
       })
  }
Share:
10,195
Dulan Hewage
Author by

Dulan Hewage

Updated on June 22, 2022

Comments

  • Dulan Hewage
    Dulan Hewage almost 2 years

    in my function I have two where clauses. what I want is to check whether a document exits, where two ids are found. but when I run it, it returns all the records of collection. can anybody tell me where I messed up?

    setApplyStatus() {
       var query = firebase.firestore().collection('applications')
       query.where("jobSeekerId", '==', this.jobSeekerId).get()
       query.where("jobId", '==', this.job.id)
       query.get().then(querySnapshot => {
        querySnapshot.forEach(doc => {
         console.log(doc.data())
         console.log('already exists')
         this.applyStatus = true
        })
       })
     }