The condition is not established by the Firestore where clause

I'm working on a function that includes two where clauses. My objective is to verify the existence of a document based on the presence of two specific IDs. However, when I execute the function, it retrieves all the records in the collection instead. Can someone point out where I might have made an error?

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

Answer №1

You're not correctly chaining the query clauses, and calling get() in the middle of your chain may not produce the desired result. To ensure that each query object builds on the last and to avoid premature retrieval of data, you should only call 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
    })
  })
}

Answer №2

Attention to all new viewers in 2021! Please note that the code has been updated as follows: PS. The ""==" condition is now replaced with 'isEqualTo: parameter'

void updateApplicationStatus() {
    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('Application already exists')
         this.applicationStatus = true
         })
       })
  }

Answer №3

Don't forget to create an index in Firebase for your query to ensure successful retrieval of results. The link to create the index can be found in the error message output on the console, so make sure to check it.

Answer №4

updateApplyStatus() {
  let searchQuery = firebase.firestore().collection('applications')
  searchQuery = searchQuery.where("jobSeekerId", '==', this.jobSeekerId)
  searchQuery = searchQuery.where("jobId", '==', this.job.id)
  searchQuery.get().then(querySnapshot => {
    querySnapshot.forEach(document => {
     console.log(document.data())
     console.log('record found')
     this.applyStatus = true
    })
   })
 }

Answer №5

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

Alternatively,

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

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Display loading animation when page is loading in a Nuxt application

I am currently working on a Nuxt project that is functioning properly. However, there is an async method that runs during the page loading process. import charge from '~plugins/charge' export default { asyncData (context, callback) { ...

An error was encountered: SyntaxError - An unexpected token '!' was found

I am having trouble creating a react cluster map. I encountered a SyntaxError, and I'm not sure what went wrong. Initially, my map was working fine, but after using the use-supercluster npm package, it started showing an Uncaught SyntaxError: Unexpect ...

Higher Order Component for JSX element - displaying JSX with wrapped component

I am looking to utilize a ReactJS HOC in order to implement a tooltip around JSX content. The function call should look similar to this: withTooltip(JSX, "very nice") To achieve this, I have created the following function: import React from "re ...

Issue detected with XMLHttpRequest - "The requested resource does not have the 'Access-Control-Allow-Origin' header."

Currently, I am working on the frontend development of an application using Angular 2. My focus is on loading an image from a third-party site via XMLHttpRequest. The code I have implemented for this task is as follows: loadFile(fileUrl) { const ...

Having trouble utilizing Reactjs Pagination to navigate through the data

I'm currently working on implementing pagination for a list of 50 records, but I'm encountering an issue. Even though I have the code below, it only displays 10 records and I'm unaware of how to show the next set of 10 records until all 50 a ...

What is the best way to retrieve dates from a MySQL database using ExpressJS?

My current task involves retrieving the date value from a MySQL server, but upon printing the result, I encounter an error. In my code, I am able to fetch the date value, however, when attempting to print it, there seems to be an issue with the output. (F ...

React not showing multiple polylines on the screen

I'm currently working on an application aimed at practicing drawing Kanji characters. To draw the lines, I'm utilizing svg. The issue I've encountered is that when I try to draw multiple separate lines using a 2D array of points for each lin ...

Having trouble getting a constructor to function properly when passing a parameter in

Here's the code snippet I'm working with: import {Component, OnInit} from '@angular/core'; import {FirebaseListObservable, FirebaseObjectObservable, AngularFireDatabase} from 'angularfire2/database-deprecated'; import {Item} ...

Verifying the authenticity of a Stripe discount code

My goal is to allow users to apply a coupon code on my website, triggering a check in the Stripe dashboard. Currently, I have implemented the following code snippet, but I am facing an issue with transferring the validCoupon data from the client side to th ...

Securely encoding information with PHP and decrypting it using JavaScript

I'm currently working with 2 servers. My goal is to generate a pair of keys, store the private key in local storage, and send the public key to my PHP server. The main objective is to encrypt data using the public key in PHP and decrypt it using Jav ...

I am facing an issue with file manipulation within a loop

I need to set up a folder with different files each time the script runs. The script should not run if the folder already exists. When I run the script, an asynchronous function is called and one part of this function causes an issue... This is my code : ...

Beware, search for DomNode!

I attempted to create a select menu using material-ui and React const SelectLevelButton = forwardRef((props, ref) => { const [stateLevel, setStateLevel] = useState({ level: "Easy" }); const [stateMenu, setStateMenu] = useState({ isOpen ...

Clicking elements reveal but page height remains unchanged?

Clicking on a label within #row-product_id_page-0-0 triggers the display of #row- elements as shown in the code snippet below: $('#row-product_id_page-0-0 label').click(function() { var str = $(this).find('.am-product-title').text ...

Updating Array Values in AngularJS based on Input Text Box Modifications

In my application, there is a text box that looks like this - <input type="text" class="form-control" id="inputID" name="ItemId" ng-model="inputItemId" ng-required="true" ng-blur="addValueToArray(inputItemId)"/> The user has the ability to add or r ...

You cannot employ typed arguments in combination with Typescript within the VueJS framework

I'm struggling to develop a typescript vue component with some methods. Here is the script snippet. <script lang="ts"> import Vue from 'vue'; export default Vue.extend({ methods: { check(value: number) { console.log(valu ...

Issue: unable to inject ngAnimate due to uncaught object error

Whenever I attempt to inject 'ngAnimate' into my app, I encounter issues with instantiation. Here is the code snippet in question: var app = angular.module('musicsa', [ 'ngCookies', 'ngResource', 'ngSanit ...

Creating interactive editable columns in Vuetify data tables

I have been working on creating a Vue data table component, and my goal is to allow for editable columns based on an array of column names provided. I found inspiration in this example, where values in specified columns are editable. In order to achieve th ...

Set up global variables for components to access

Currently, I am working on a Laravel 8 project with vue-sweetalert2 integration. My goal is to set up the Toast behavior once and then be able to call it within various components. At the moment, my code looks like this: /js/components/Mypage.vue <scr ...

Create dynamic animations using AngularJS to transition between different states within an ng-repeat loop

Here's a simplified explanation of my current dilemma: I have an array containing a list of items that are being displayed in an Angular view using ng-repeat, like... <li ng-repeat="item in items"> <div class="bar" ng-style="{'width ...

Guide to inserting a YouTube video or any iframe using vue-dompurify-html

Recently, I completed a blogging project in Nuxt where I utilized the quill text editor for managing content in the description field of my database. However, upon trying to display the blog post's description from the database using v-html, I encoun ...