Swift Sharp
Swift Sharp

Reputation: 2623

Firestore - Querying reference field

I am working on a small iOS app, using Swift and Cloud Firestore database.

I have collection (A) each document of which contains field with reference to another collection(B). Why question is how to query that field?

  1. I am getting references to B by performing a simple query
     let doc =  "merchant"

     let ref =  db.collection(doc).whereField("subscribers",arrayContains: Session.current.user!.uid!)
  1. Then what I am trying to do is to compare the value from ref with reference field from collection A like so
   db.collection("appointment").whereField("merch_ref", isEqualTo: ref).addSnapshotListener { snapshot, error in

        if let snap = snapshot {
            for item in snap.documents {
                print(item.data())
            }
        }
    }

When I execute that I'm getting an error

Terminating app due to uncaught exception 'FIRInvalidArgumentException', reason: 'Unsupported type: FIRQuery'

Can you point me in the right direction

Thanks

Upvotes: 0

Views: 1861

Answers (2)

Quins
Quins

Reputation: 813

Here is how I use reference type to query a collection (node.js):

let myCollectionADocument = await admin.firestore().collection("collection_a").doc("documentId").get();
let myCollectionB = await admin.firestore().collection("collection_b").where("collection_a_id", "==", myCollectionADocument.ref).get();

Upvotes: 2

Doug Stevenson
Doug Stevenson

Reputation: 317372

Your ref isn't actually a document reference type object. The compiler is telling you it's a Query type object. It just contains all the parameters for the query you defined. It doesn't actually have any document data in it.

If you want to get the matching documents from that query object, you should follow the instructions in the documentation. You will need to execute the query, iterate the result snapshots, and use those results as a basis for comparison.

Upvotes: 0

Related Questions