微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

Firebase 实时数据库 setValue() 不起作用

如何解决Firebase 实时数据库 setValue() 不起作用

我正在尝试将有关用户的一些基本信息写入 Firebase 实时数据库,但在运行 setValue() 时,控制台中没有任何反应,经过数小时的尝试后,我仍然无法找出问题所在。

>

这是调用 registerUser 函数的片段中的代码

registerButton.setonClickListener {

        firstNameLayout.error = null
        lastNameLayout.error = null
        emailFieldLayout.error = null
        passwordFieldLayout.error = null

        var fieldCheck = true

        if(TextUtils.isEmpty(firstNameField.text)) {
            firstNameLayout.error = "Please Enter First Name";
            fieldCheck = false
        }
        if (TextUtils.isEmpty(lastNameField.text)) {
            lastNameLayout.error = "Please Enter Last Name"
            fieldCheck = false
        }
        if(TextUtils.isEmpty(emailField.text)) {
            emailFieldLayout.error = "Please Enter Email Address";
            fieldCheck = false
            // Todo: Make sure format is correct
        }
        if(TextUtils.isEmpty(passwordField.text)){
            passwordFieldLayout.error = "Please Choose a Password"
            fieldCheck = false
            // Todo: Stricter password requirements 
        }
        if(!fieldCheck) {
            return@setonClickListener
        }

        registerButton.startAnimation()

        val fName = firstNameField.text.toString().trim()
        val lName = lastNameField.text.toString().trim()
        val email = emailField.text.toString().trim()
        val password = passwordField.text.toString().trim()

        try {
            registerButton.doneLoadingAnimation(2,bitmap)
            (activity as LoginRegister).registerUser(email,password,fName,lName)

        } catch (e: Exception) {
            Toast.makeText(activity,e.message,Toast.LENGTH_SHORT).show()
            registerButton.revertAnimation()
        }

    }

这是来自片段父活动的 registerUser 函数体:

fun registerUser(email: String,password: String,fName: String,lName: String) {
    //Registrerer med Firebase Authentication
    auth.createuserWithEmailAndPassword(email,password)
        .addOnCompleteListener(this) { task ->
            if (task.isSuccessful) {
                // Sign in success,update UI with the signed-in user's information
                Log.d(TAG,"createuserWithEmail:success")
                val user : FirebaseUser = task.result!!.user!!
                val uid: String = user.uid
                val dbUser = User(fName,lName,email)

                writeNewUser(uid,dbUser)

                val intent = Intent(this@LoginRegister,MainActivity::class.java)
                intent.flags = Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_CLEAR_TASK
                intent.putExtra("user_email",email)
                intent.putExtra("user_first_name",fName)
                intent.putExtra("user_last_name",lName)
                startActivity(intent)
                finish()

            } else {
                // If sign in fails,display a message to the user.
                Log.w(TAG,"createuserWithEmail:failure",task.exception)
                Toast.makeText(baseContext,"Authentication Failed.",Toast.LENGTH_SHORT).show()
            }
        }



}

这是 writeNewUser 函数

fun writeNewUser(userId: String,user: User) {
    database = FirebaseDatabase.getInstance().getReference("Users").child(userId)
    database.child("users").child(userId).setValue(user)
}

User 对象是从这个 kotlin 类实例化的:

data class User(val fName: String? = null,val lName: String? = null,val email: String? = null) {
val firstName = fName?.capitalize(Locale.ROOT)
val lastName = lName?.capitalize(Locale.ROOT)
val emailAddress = email

}

Firebase:

Firebase nodes

有人知道这背后的原因吗?

解决方法

fun writeNewUser(userId: String,user: User) {
    database = FirebaseDatabase.getInstance().getReference("Users").child(userId)
    database.child("users").child(userId).setValue(user)
}

我猜你在上面的代码片段中复制了几个子节点,因为这意味着一组用户有一个用户,而这个特定用户有一组用户,没有意义,所以你可以删除这些重复项:

fun writeNewUser(userId: String,user: User) {
    database = FirebaseDatabase.getInstance().getReference("Users").child(userId)
    database.setValue(user)
}

更新:

enter image description here

firebase 节点区分大小写,因此将“Users”更改为小写的“users”

fun writeNewUser(userId: String,user: User) {
    database = FirebaseDatabase.getInstance().getReference("users").child(userId)
    database.setValue(user)
}
,

我解决了这个问题,我使用的不是默认的数据库位置,我没有发现我需要在 getInstance() 方法中传递数据库 URL 的事实。传递正确的数据库 url 修复了问题

,

将数据库的 URL 放在 getInstance() 中是我的解决方案,现在它完美无缺

示例:

private val database = FirebaseDatabase.getInstance("https://conect-c91b3-default-rtdb.firebaseio.com/").getReference()

// to test if is working
database.child("users").setValue("test")

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其他元素将获得点击?
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。)
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbcDriver发生异常。为什么?
这是用Java进行XML解析的最佳库。
Java的PriorityQueue的内置迭代器不会以任何特定顺序遍历数据结构。为什么?
如何在Java中聆听按键时移动图像。
Java“Program to an interface”。这是什么意思?