0

I've read several articles here on StackOverflow asking this same question and the answers are all the same but it doesn't seem to work for me and I'm not sure why. I've read Elegant Way To Dynamically Query FireStore, Conditional Where Query on Firestore, the two articles that question references, plus How to conditionally add another filter to a query in Firebase Firestore using swift?, and the article Firestore Query Options Swift.

I followed the simple guidance the all offered:

var ref = db.collection("Violations")

if locationCompoundQuery != "" {
            ref = ref.whereField("location", isEqualTo: locationCompoundQuery)
        }

The issue is that Xcode gives me an error: Cannot assign value of type 'Query' to type "CollectionReference" insert 'as! CollectionReference'. If I do that it will compile and run, but when the query runs it crashes. The message in the console is: Could not cast value of type 'FIRQuery' (0x108972150) to 'FIRCollectionReference'

I'm using Xcode 13.1. All of my other queries that are compound without conditional statements run just fine. Any help is greatly appreciated.

JimD
  • 1
  • 1
  • Well, the error is on point. `ref` is a reference to a collection and `ref.whereField` is a query. Try `let query = ref.whereField("location", isEqualTo: locationCompoundQuery)`. Not sure what this `locationCompoundQuery` resolves to but that could be a problem as well. If that doesn't fix it, update your question with more complete information – Jay Jan 27 '22 at 21:17

0 Answers0