【问题标题】:Firebase Database not storing data, logcat shows no errorFirebase 数据库不存储数据,logcat 没有显示错误
【发布时间】:2018-08-13 16:47:22
【问题描述】:

我正在学习 Kotlin,并在用户登录应用程序时尝试将用户的电子邮件和 UID 保存在 firebase 数据库中,但数据没有存储在数据库中,并且 logcat 没有显示错误,所以我不知道是什么问题。

附: - 我已将 firebase 数据库规则设置为

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

这是 login.kt 文件的代码

package com.revorg.tictactoe

import android.content.Intent
import android.support.v7.app.AppCompatActivity
import android.os.Bundle
import android.view.View
import android.widget.Toast
import com.google.firebase.auth.FirebaseAuth
import com.google.firebase.auth.FirebaseUser
import com.google.firebase.database.FirebaseDatabase
import kotlinx.android.synthetic.main.activity_login.*

class Login : AppCompatActivity() {

var mFirebaseAuth: FirebaseAuth?=null

var database = FirebaseDatabase.getInstance()
var myRef=database.reference

override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setContentView(R.layout.activity_login)

    mFirebaseAuth= FirebaseAuth.getInstance()
}

fun buLoginEvent(view:View){

    LoginToFirebase(etEmail.text.toString(),etPassword.text.toString())
}

fun LoginToFirebase(email:String,password:String){

    mFirebaseAuth!!.createUserWithEmailAndPassword(email,password)
            .addOnCompleteListener(this) {task ->

                if(task.isSuccessful){
                    Toast.makeText(applicationContext,"Login Successful",Toast.LENGTH_SHORT).show()
                    var currentuser = mFirebaseAuth!!.currentUser
                    //save to Database
                    if(currentuser!=null) {
                        myRef.child("Users").child(splitString(currentuser.email.toString())).child(currentuser.uid)
                    }

                    LoadMain()

                } else {
                    Toast.makeText(applicationContext,"Login Failed", Toast.LENGTH_SHORT).show()
                }
            }

}

override fun onStart() {
    super.onStart()

    LoadMain()
}

fun LoadMain(){

    var currentuser = mFirebaseAuth!!.currentUser

    if(currentuser!=null) {


        var intent = Intent(this, MainActivity::class.java)
        intent.putExtra("email", currentuser.email)
        intent.putExtra("uid", currentuser.uid)

        startActivity(intent)
    }
}

fun splitString(str:String):String{

    var split = str.split("@")
    return split[0]
}
}

【问题讨论】:

  • Toast 是否显示:“登录成功”或不成功?
  • 你检查答案了吗?
  • @PeterHaddad 成功了!非常感谢
  • @Mohsen 显示“登录成功”,问题出在 PeterHaddad 建议的范围内,我没有使用“setValue()”,而只是使用“child()”。

标签: android firebase firebase-realtime-database kotlin


【解决方案1】:

什么都没有被存储,因为你没有存储任何东西,你需要使用setValue()来存储数据。 child() 获取对数据库中某个位置的引用。

你应该这样做:

myRef.child("Users").child(currentuser.uid).child("email").setValue(email);

那么你就会有这个数据库:

Users
  userUid
     email: email_here

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-03-03
    • 2017-05-15
    • 2021-08-22
    • 2020-05-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多