将数据添加到实时数据库

时间:2019-03-18 21:27:09

标签: android firebase-realtime-database kotlin

我正在尝试从我的android应用中将用户添加到Firebase存储中,我创建了一个User类:

class User {
    var userId:String = ""
    var firstName: String = ""
    var lastName :String = ""
    var address: String = ""
    var phoneNumber:String = ""
    var email:String = ""
    var password :String = ""
    constructor(fName:String,lName:String,address:String,userId:String,phoneNumber:String,email:String,password:String) {
        this.userId = userId
        this.firstName = fName
        this.lastName = lName
        this.address = address
        this.phoneNumber = phoneNumber
        this.email = email
        this.password = password
    }
}

然后创建用户,成功创建用户后,我想将用户保存在存储中,如下代码:

authRef!!.createUserWithEmailAndPassword(email,password).addOnCompleteListener(this) {  task ->
                if(task.isSuccessful){
                    val userId = authRef!!.currentUser!!.uid
                    val user:User = User(firstName,lastName,address,userId,phoneNumber,email,password)
                    databaseRef!!.child(userId).setValue(user).addOnCompleteListener{
                        if(it.isSuccessful){
                            println("success")
                            Toast.makeText(this,"successs",Toast.LENGTH_LONG).show()
                        }else {
                            Toast.makeText(this,"error",Toast.LENGTH_LONG).show()
                            println("error")
                        }
  }

但是没有保存数据,知道什么地方出错了吗? enter image description here

enter image description here

1 个答案:

答案 0 :(得分:0)

您放弃写入权限了吗?尝试在 项目->数据库->规则 中使用此代码,然后粘贴此代码

{
  "rules": {
    ".read": false,
    ".write": false
  }
}

访问https://firebase.google.com/docs/database/security,以了解有关安全规则的更多信息。