Home > Enterprise >  Cannot add data to different nodes in firebase
Cannot add data to different nodes in firebase

Time:09-27

In Firebase Realtime Database, already have user node. I need to create another node named BankDetails but when I try to enter details It gets an error. not created Another node.

Firebase

New Created Bank Details

Here's My code. It's on the fragment.

val database = Firebase.database
    val myRef = database.getReference("BankDetails")
 view.rechargeAmount.setOnClickListener {

            if( emailBankText.text.trim().isNotEmpty()) {
                val bankAccNo = bankAccText.getText().toString()
                val accountBalance = amountText.getText().toString().toDouble()

                val encodedEmail = encodeUserEmail(emailBankText.toString())
                val bankDetails1 = BankDetails(encodedEmail,bankAccNo,accountBalance)
                myRef.child(encodedEmail).setValue(bankDetails1).addOnSuccessListener {

                    Toast.makeText(activity, "updated successfully", Toast.LENGTH_LONG).show()


                } .addOnFailureListener{

                    Toast.makeText(activity, "not updated", Toast.LENGTH_LONG).show()

                }
            }

            else{
                Toast.makeText(activity, "error", Toast.LENGTH_LONG).show()
            }

This's the error I got com.google.firebase.database.DatabaseException: Invalid Firebase Database path Is there anything wrong in my code. Help me, I'm new to Kotlin.

edit

I created another node and add some test data. Like this but

I still got this error

CodePudding user response:

BankDetails does not exist, so myRef variable cannot be initialized because they can't find this node, you need to create this node by adding some test data inside the node and then the problem should be fixed.

CodePudding user response:

The problem in your code is due to the use of a . inside a value that will be added as a key in the Realtime Database, which is actually not possible. You should always encode the email address to:

[email protected] -> name@email,com

As explained in my answer from the following post:

How to add email address to firebase kotlin?

  • Related