0

I translated the code below from java, but I am getting an error in some places, why is it giving an error, is there a point I missed?

val mQuery: Query = firestore.collection("users")
                        .whereEqualTo("nickname", mUserName)

mQuery.addSnapshotListener(object : EventListener<QuerySnapshot> {
    fun onEvent(
        documentSnapshots: QuerySnapshot,
        e: FirebaseFirestoreException?
    ) {
        for (ds in documentSnapshots) {
            if (ds != null) {
                val userName: String = document.getString("username")
                Log.d(
                    TAG,
                    "checkingIfusernameExist: FOUND A MATCH: $userName"
                )
                Toast.makeText(
                    this@SignUpActivity,
                    "That username already exists.",
                    Toast.LENGTH_SHORT
                ).show()
            }
        }
    }
})

I've been doing the things described here for 2-3 days, but it keeps throwing an error.Event listeners and docs throw errors.

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

1 Answers1

0

I translated the code below from java

That's not the correct way of "translating" that code from Java to Kotlin, since that answer provides a solution for getting data only once. So to be able to do that in Kolin programming language please use the following lines of code:

val rootRef = FirebaseFirestore.getInstance()
val allUsersRef = rootRef.collection("all_users")
val userNameQuery = allUsersRef.whereEqualTo("username", "userNameToCompare")
userNameQuery.get().addOnCompleteListener { task ->
    if (task.isSuccessful) {
        for (document in task.result) {
            if (document.exists()) {
                Log.d("TAG", "username already exists")
                val userName = document.getString("username")
                //Do what you need to do with the userName
            } else {
                Log.d("TAG", "username does not exists")
            }
        }
    } else {
        Log.d("TAG", "Error getting documents: ", task.exception)
    }
}
Alex Mamo
  • 130,605
  • 17
  • 163
  • 193
  • Hello, if the username is not registered, where should I add my transactions?(Yes, you helped a lot, but since I'm new here, when I click that button, it says raise your reputation.When my reputation rises, I will click the positive button on your other answers.) – Grigroviska Oct 14 '21 at 14:31
  • "username does not exist" this is why I can't enter – Grigroviska Oct 14 '21 at 14:54
  • If the `username` doesn't exist, it means that the else part of the if statement is triggered. If you need to take some action. then add that logic there. Right? – Alex Mamo Oct 15 '21 at 05:54
  • Dear Alex, I'm really grateful for your reply. I have upvoted it. But since I just signed up, the system will start taking my votes once I reach a certain reputation count. – Grigroviska Oct 16 '21 at 17:08
  • 1
    I get it now.Sorry. – Grigroviska Oct 18 '21 at 09:43