如何解决Firebase 实时数据库 setValue() 不起作用
我正在尝试将有关用户的一些基本信息写入 Firebase 实时数据库,但在运行 setValue()
时,控制台中没有任何反应,经过数小时的尝试后,我仍然无法找出问题所在。
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:
有人知道这背后的原因吗?
解决方法
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)
}
更新:
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 举报,一经查实,本站将立刻删除。