real4you
real4you

Reputation: 31

How can I read an updated value from firebase?

I am trying to read a String field that I recently updated. My function that should return the String, returns an empty String.

Here is my codes:

Function that returns the updated String:

fun readUsersOfficeHoursList(email: String, callback: (String) -> Unit) {
    val database = FirebaseFirestore.getInstance()
    val ref = database.collection("Users").document(email)

    ref.get()
        .addOnSuccessListener { document ->
            if (document != null) {
                val officeHoursList = document.get("office_hours_list") as String
                callback(officeHoursList)
                Log.d(TAG, "office_hours_list successfully read")
            } else {
                Log.d(TAG, "Is empty")
                callback("")
            }
        }
        .addOnFailureListener { exception ->
            if (exception is FirebaseFirestoreException) {
                Log.e(TAG, "Error getting document: ", exception)
            }
            callback("")
        }
}

Function that updates the field:

fun updateUserOfficeHoursList(email: String, code: String){
    val database = FirebaseFirestore.getInstance()
    val ref = database.collection("Users").document(email)
    var list = ""

    ref.get()
        .addOnSuccessListener { document ->
            if (document != null) {
                list = document.get("office_hours_list") as String? ?: ""
                Log.d(TAG, "office_hours_list successfully read")
                if(!list.contains(code)){
                    if (list.isEmpty()){
                        list = code
                    }
                    else{
                        list = "$list, $code"
                    }
                    ref.update("office_hours_list", list)
                        .addOnSuccessListener { Log.d(TAG, "List successfully updated") }
                        .addOnFailureListener { e -> Log.w(TAG, "Error updating list", e) }
                }else{
                    Log.d(TAG, "code already in the list")
                }
            } else {
                Log.d(TAG, "Is empty")
            }
        }
        .addOnFailureListener { exception ->
            if (exception is FirebaseFirestoreException) {
                Log.e(TAG, "Error getting document: ", exception)
            }
        }
}

My test code:

myClass.updateUserOfficeHoursList("[email protected]", "1VVNFxSGbYaauk3iLV80, 
1a79bhnaJsY5OhHwaYhH")
        myClass.readUsersOfficeHoursList("[email protected]") {fieldValue1 ->
            textView.text = fieldValue1
            Log.d(TAG, "fieldValue1: $fieldValue1")
        }

The error I get:

**2023-01-16 13:43:50.523 8529-8529/com.example.myapplication E/AndroidRuntime: FATAL EXCEPTION: main Process: com.example.myapplication, PID: 8529 java.lang.NullPointerException: null cannot be cast to non-null type kotlin.String at com.example.myapplication.RepositoryMockup$readUsersOfficeHoursList$1.invoke(RespositoryMockup.kt:239) at com.example.myapplication.RepositoryMockup$readUsersOfficeHoursList$1.invoke(RespositoryMockup.kt:237) at com.example.myapplication.RepositoryMockup.readUsersOfficeHoursList$lambda$15(RespositoryMockup.kt:237) at com.example.myapplication.RepositoryMockup.$r8$lambda$Rz1CeV4qQ243JiYTVZ8j2Ijj1y0(Unknown Source:0) at com.example.myapplication.RepositoryMockup$$ExternalSyntheticLambda16.onSuccess(Unknown Source:2) at com.google.android.gms.tasks.zzm.run(com.google.android.gms:play-services-tasks@@18.0.1:1) at android.os.Handler.handleCallback(Handler.java:942) at android.os.Handler.dispatchMessage(Handler.java:99) at android.os.Looper.loopOnce(Looper.java:201) at android.os.Looper.loop(Looper.java:288) at android.app.ActivityThread.main(ActivityThread.java:7898) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:548) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:936) **

Users Collection

OfficeHoursInstanceCollection

Upvotes: 1

Views: 41

Answers (1)

Frank van Puffelen
Frank van Puffelen

Reputation: 599131

This is the wrong way to check whether a document you tried to read exists:

if (document != null) {

When no document exists, the Firestore API returns a DocumentSnapshot (so not null) with its exists property set to false. So the correct check would be:

if (document.exists) {

Upvotes: 1

Related Questions