【问题标题】:Firestore where clause doesn't make the condition.Firestore where 子句不构成条件。
【发布时间】:2018-08-08 00:45:18
【问题描述】:

在我的函数中,我有两个 where 子句。我想要的是检查一个文档是否存在,其中找到了两个 id。但是当我运行它时,它会返回所有收集记录。谁能告诉我我在哪里搞砸了?

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
    })
   })
 }

【问题讨论】:

    标签: javascript firebase vue.js google-cloud-firestore


    【解决方案1】:

    您没有正确链接查询子句。此外,您在链的中间调用 get() 。这几乎肯定不是你想要的。每个查询对象都建立在最后一个查询对象上,您应该只在链中的最后一个查询上使用 get():

    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
        })
      })
    }
    

    【讨论】:

      【解决方案2】:

      尝试在 firebase 上为您的查询创建一个索引,因为没有索引它将无法显示您的结果。您可以获取在控制台中创建索引的链接出错,因此请检查您的控制台

      【讨论】:

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

        【讨论】:

          【解决方案4】:

          citiesRef.where("state", "==", "CA", "||", "state", "==", "AZ")

          或者更好

          citiesRef.where("state == CA || state == AZ") 来源:Firebase GIT

          【讨论】:

            【解决方案5】:

            对于 2021 年观看此内容的新用户。代码如下: PS。不再有 "==" 子句,现在是 '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
                     })
                   })
              }
            

            【讨论】:

              猜你喜欢
              • 2018-06-10
              • 2018-07-08
              • 1970-01-01
              • 2021-11-01
              • 1970-01-01
              • 1970-01-01
              • 2015-05-15
              相关资源
              最近更新 更多