Home > database >  Cannot Update Firebase data ? Kotlin
Cannot Update Firebase data ? Kotlin

Time:09-26

I cannot update firebase data using kotlin. Here's my button click listener. This' s fragment. So I Include this code in on View Created

 fun decodeUserEmailUpdate (emailAddressText: EditText) = emailAddressText.replace(",", ".")
    fun encodeUserEmail(emailAddressText: EditText) = emailAddressText.replace(".", ",")

    view.updateProfile.setOnClickListener { view ->
                val encodedEmail = encodeUserEmail(emailAddressText)
                val decodeUserEmailUpdate = decodeUserEmailUpdate(emailAddressText)
                val reference = FirebaseDatabase.getInstance().getReference("User")
                val checkUser = reference.orderByChild("emailAddress").equals(decodeUserEmailUpdate)

                if(checkUser != null) {

                    "fullName" to nameText.text.toString()
                    "age" to ageText.text.toString()
                    "bankAccNo" to bankText.text.toString()
                    "emailAddress" to encodedEmail
                    "phoneNo" to phoneText.text.toString()
                    "passwordRegister" to passwordText.text.toString()
                }
                else{

                }
        }

CodePudding user response:

The way you are trying to update the data is wrong.

what is happening here is:

   view.updateProfile.setOnClickListener { view ->
            val encodedEmail = encodeUserEmail(emailAddressText)
            val decodeUserEmailUpdate = decodeUserEmailUpdate(emailAddressText)

            /**
             *  You get the Reference of Firebase Database "User"
             */

            val reference = FirebaseDatabase.getInstance().getReference("User")

            val checkUser = reference.orderByChild("emailAddress").equals(decodeUserEmailUpdate)
            
            /**
             *  You check whether checkUser is null or not
             *  checkUser will always be not equal to null, so its useless.
             *  Also, the below code doesn't do anything.
             */
            if (checkUser != null) {

                "fullName" to nameText.text.toString()
                "age" to ageText.text.toString()
                "bankAccNo" to bankText.text.toString()
                "emailAddress" to encodedEmail
                "phoneNo" to phoneText.text.toString()
                "passwordRegister" to passwordText.text.toString()
            } else {

            }
        }

What you are missing out on is, using the reference to set the data. For example: If your structure looks something like this Root--> User --> Email(ex. "[email protected]")

Your code should look something like this:

        val encodedEmail = encodeUserEmail(emailAddressText)
        val decodeUserEmailUpdate = decodeUserEmailUpdate(emailAddressText)

        /**
         *   get the reference of path where updates need to be carried out.
         */
        val reference = FirebaseDatabase.getInstance()
            .getReference("User")
            .child(decodeUserEmailUpdate)

        /**
         *   I believe, this is not required
         */
        val checkUser = reference.orderByChild("emailAddress").equals(decodeUserEmailUpdate)


        /**
         * Create a map of data that needs to be updated
         */
        val data = mapOf<String, Any>(
            "fullName" to nameText.text.toString(),
            "age" to ageText.text.toString(),
            "bankAccNo" to bankText.text.toString(),
            "emailAddress" to encodedEmail,
            "phoneNo" to phoneText.text.toString(),
            "passwordRegister" to passwordText.text.toString()
        )

        try {
            /**
             * Use your reference to update the children of the reference you got before
             */
            reference.updateChildren(data).addOnCompleteListener {
                if (it.isSuccessful) Log.d("FirebaseDatabase", "Data Updated")
                if (it.exception != null) Log.e("FirebaseDatabase", "Update Error", it.exception)
            }
        }
        /**
         * Catch any exceptions that might occur
         */
        catch (e: Exception) {
            Log.e("FirebaseDatabaseUpdate", "ERROR", e)
        }

Feel free to ask if you have any further questions.

  • Related