Home > other >  Firebase Realtime database not updating data
Firebase Realtime database not updating data

Time:11-16

I am new to Firebase and I am trying to learn its database. This is what my code looks like.

import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity
import com.example.roomdemo.databinding.ActivityMainBinding
import com.google.firebase.database.FirebaseDatabase

class MainActivity : AppCompatActivity() {
    private  var binding: ActivityMainBinding? = null


    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        binding = ActivityMainBinding.inflate(layoutInflater)
        setContentView(binding?.root)

        binding?.button?.setOnClickListener {
            val rootNode = FirebaseDatabase.getInstance()
            val reference = rootNode.getReference("Users")
            reference.setValue("hello")
        }

    }
}

I am simply trying to set the value 'hello' to my node 'Users'. But Somehow it's not working. Are there any configuration settings that I missed? Please help me. Also pasting a screenshot of how the database looks like Database image

Please help to connect my code with the database

CodePudding user response:

To see if something goes wrong, you have to attach a listener and handle exceptions:

reference.setValue("hello").addOnCompleteListener {
    if (it.isSuccessful) {
        Log.d"(TAG", "Data added successfully.")
    } else {
        //Never ignore potential errors!
        Log.d("TAG", "Failed with: ${it.exception?.message}")
    }
}

If the above operation doesn't add anything to the database, it's most likely because the Firebase servers reject the operation due to improper security rules. So you have set the proper rules in your Firebase console.

  • Related