How to request a Firebase Firestore Reference data type?

I use the Firestore reference data type to store a link to a user, as shown in the screenshots below

User Link

enter image description here

User Collections

enter image description here

When I try to request this data, I get a ClassCastException (I tried to use it for String just for the sake of it).

code

 //.. this function reads data from DocumentSnapshot //.. and converts to an Organization private fun DocumentSnapshot.toOrganization(): Organization { //.. some code (this.data["members"] as ArrayList<HashMap<String, Any>>).map { toOrgMember(it) }) //.. more code } fun toOrgMember(map: Map<String, Any>): OrgMember { //map["user"] as String throws ClassCastException. Refer first screenshot return OrgMember(map["id"] as Long, UserRef(map["user"] as String), map["type"] as String, asJobTitlesList(map["jobTitles"] as String)) } 

Stacktrace

 10-14 20:31:17.503 15569-15569/com.ab W/System.err: Caused by: java.lang.ClassCastException: com.google.android.gms.internal.zzegf cannot be cast to java.lang.String 10-14 20:31:17.504 15569-15569/com.ab W/System.err: at feature.model.core.CoreUtilsKt.toOrgMember(CoreUtils.kt:28) 10-14 20:31:17.504 15569-15569/com.ab W/System.err: at feature.model.organization.OrgRemoteKt.toOrganization(OrgRemote.kt:55) 

In which class should I use the reference data type? ( com.google.android.gms.internal.zzegf seems like an inner class that should not be used)

At the moment, I have not found any example in the docs for the reference type. Any help would be appreciated.

+5
source share
3 answers

Firestore returns a DocumentReference when retrieving a link from your collections. If changing the cast in DocumentReference does not work, track this issue .

+2
source

We will need to see some of your code to give you an answer. but at the same time, this is what my bottom-query looks like, it assumes that you are looking for something unique if you can’t see the results.

 FireBaseFirestore db = FirebaseFirestore.getInstance(); collectionRef = db.collection("yourCollection"); Query query = collectionRef.whereEqualTo("Field", "yourQuery" ); query.get().addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() { @Override public void onComplete(@NonNull Task<QuerySnapshot> task) { if(task.isSuccessful()) { QuerySnapshot qSnap = task.getResult(); if (!qSnap.isEmpty()) { Log.d("Query Data", String.valueOf(task.getResult().getDocuments().get(0).getData())); } else { Log.d("Query Data", "Data is not valid"); } } } }); 
+1
source

I have the same problem. However, I can solve this problem using:

 DocumentReference docRef = firestore.document(map.get("reference_field").toString()); 

I'm not sure if this is the right way, but this way works for me.

- Update -

It seems the new version of firestore (11.6.0) on Android cannot use my path. However, it can be launched directly.

 DocumentReference docRef = (DocumentReference) map.get("reference_field"); 
+1
source

Source: https://habr.com/ru/post/1272632/


All Articles