Home > OS >  Angular Firestore query with multiple parameters error
Angular Firestore query with multiple parameters error

Time:08-11

I'm trying to implement a filtering system for vehicle data into my small project and there are 6 parameters that I'm supposed to filter through. I've looked up some solutions online but none have worked, this is the current state of my code:

//this part checks if the inputs have been filled/changed inside the HTML of the component and if they
//have, the operator becomes '==', and if not it becomes '!=' so it returns all the other values and proceeds.


const typeField = (type !== '' && type !== 'Vehicle type') ? '==' : '!='
const priceField = (price !== 0 && price !== null) ? '<=' : '!='
const registeredField = (registered !== null && registered !== undefined) ? '==' : '!='
const serviceField = (service !== null && service !== undefined) ? '==' : '!='
if(service == undefined) service = null
if(registered == undefined) registered = null
const yearField = (year !== 0 && year !== null) ? '==' : '!='
const insuranceField = (insurance !== '' && insurance !== null && insurance !== 'Insurance') ? '==' : '!='

//the query:


return this.db.collection("Vehicles", ref =>
      ref.where('type', typeField, type)
      .where('purchasePrice', priceField, price)
      .where('latestRegistrationDate', registeredField, registered)
      .where('latestMinorServiceDate', serviceField, service)
      .where('productionYear', yearField, year)
      .where('latestMandatoryInsuranceDate', insuranceField, insurance)
    ).valueChanges()

I am getting this error in the console:

FirebaseError: Invalid query. All where filters with an inequality (<, <=, !=, not-in, >, or >=) must be on the same field. But you have inequality filters on 'type' and 'purchasePrice'

Am I doing something wrong or is it simply not possible to combine queries like this? Is there any other way to do this without doing the filtering locally?

CodePudding user response:

In the constant trade-off between the types of queries that it can execute and the performance guarantees it can make, the makers of Firestore so far have opted to stick to their performance guarantee. This means that some queries you may be used to on other databases, are not possible on Firestore.

As AyseAsude commented and the error message says, a Firestore query can only contains range conditions (which includes !=) on a single field. That is a limitation of the database and there's no workaround.

If this query type is a hard requirement for your application, you may want to consider a database that better matches with your query needs.

  • Related