2

I am developing an android native application with Kotlin and Firebase , I have a collection named Topic and 2 fields of type reference one for User and the other for Category , I was trying to get all documents also with the reference document , but it does not seem to work:

db.collection("topic").get().addOnSuccessListener { result ->
        for (document in result) {
            Log.e("success", "${document.id} => ${document.data.get("subject")}")
            var topic: Topic = Topic(
                document.id as String,
                document.data.get("subject") as String,
                document.data.get("content") as String,
                document.data.get("created_at") as String,
                document.getDocumentReference("Category") as Category,
                document.getDocumentReference("User") as User
            )}

No error , but no result either in my print(topic) .

This is Topic Class

class Topic : Serializable {
    var id : String = ""
    var subject : String = ""
    var content : String = ""
    var created_at : String = ""
    var cat = Category()
    var user = User()

    constructor(){}

    constructor(
        id: String,
        subject: String,
        content: String,
        created_at: String,
        cat: Category,
        user: User
    ) {
        this.id = id
        this.subject = subject
        this.content = content
        this.created_at = created_at
        this.cat = cat
        this.user = user
    }

    override fun toString(): String {
        return "Topic(id='$id', subject='$subject', content='$content', created_at='$created_at', cat=$cat, user=$user)"
    }
}

Database

Alex Mamo
  • 130,605
  • 17
  • 163
  • 193

2 Answers2

1

You'll have to get each referenced document explicitly with a separate get() call. There's no way to get them automatically in your current call.

So something like:

db.collection("topic").get().addOnSuccessListener { result ->
    for (document in result) {
        document.getDocumentReference("Category").get().addOnSuccessListener { categoryDoc ->
            let category = categoryDoc.data as Category
            ...
        }
    }
}

Also see:

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
1

When you are using the following line of code:

document.getDocumentReference("Category") as Category

You are getting an object of type DocumentReference and not an object of type Category because DocumentSnapshot's getDocumentReference() method returns that kind of object. Remember that in Kotlin, there is no way you can cast an object of type DocumentReference to Category, hence that behaviour.

So the assumption that simply getting a reference to an object will return the object itself is not correct. The only way in which you can solve this is to make separate calls for each one of them, as @FrankvanPuffelen mentioned in his answer.

As a separate topic, you can also take a look at my answer from the below post, as I saw that you are using different naming for the properties in the document:

Alex Mamo
  • 130,605
  • 17
  • 163
  • 193