从Firebase实时数据库读取,其中参考=用户UID

时间:2020-04-10 11:50:35

标签: android firebase kotlin firebase-realtime-database firebase-authentication

我正在通过身份验证创建新用户,并将数据库中的用户密钥设置为用户UID。

private fun writeNewUser(name: String, email: String?) {
        val user = User(name, email)
        mDatabase.child("Users").child(mAuth.currentUser!!.uid).setValue(user)
}



这可以正常工作并产生以下结果: Authentication + Write Database Result

我现在正尝试登录并阅读该用户的信息。

我正在尝试将UID设置为数据库引用(成功登录后)。
*我的登录过程正在运行。

我在尝试显示数据的MainActivity上遇到错误:

错误 : 使用提供的参数无法调用以下函数:

线路 : 键?.let {getValue(mAuth.uid)}

这是我要尝试读取和显示数据的MainActivity代码。

class MainActivity : AppCompatActivity() {

    private lateinit var mUser: User
    private lateinit var mAuth : FirebaseAuth
    private lateinit var mDatabase: DatabaseReference

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

        mAuth = FirebaseAuth.getInstance()
        mDatabase = FirebaseDatabase.getInstance().reference

        fun currentUserReference(): DatabaseReference =
            mDatabase.child("users").child(mAuth.currentUser!!.uid)

        currentUserReference().addListenerForSingleValueEvent(
            ValueListenerAdapter{
                mUser = it.asUser()!!
                tv_main_name.setText(mUser.name)
                tv_main_email.setText(mUser.email)
            }
        )

    }

    fun DataSnapshot.asUser(): User? =
        //error on this line ↓
        key?.let { getValue(mAuth.uid) }


}



编辑1

现有的ValueListenerAdapter.kt


class ValueListenerAdapter(val handler: (DataSnapshot) -> Unit): ValueEventListener {

    private val TAG = "ValueListenerAdapter"

    override fun onDataChange(data: DataSnapshot) {
        handler(data)
    }

    override fun onCancelled(error: DatabaseError) {
        Log.e("onCancelled", TAG, error.toException())
    }
}



编辑2

class MainActivity : AppCompatActivity() {

    private lateinit var mUser: User
    private lateinit var mAuth : FirebaseAuth
    private lateinit var mDatabase: DatabaseReference

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

        mAuth = FirebaseAuth.getInstance()
        mDatabase = FirebaseDatabase.getInstance().reference

        mDatabase.child("Users").child(mAuth.currentUser!!.uid)
             .addListenerForSingleValueEvent(object : ValueEventListener {

                    override fun onCancelled(p0: DatabaseError) {
                        TODO("Not yet implemented")
                    }

                    override fun onDataChange(p0: DataSnapshot) {

                        //Declare mUser as chosen DB User

                        tv_main_name.setText(mUser.name)
                        tv_main_email.setText(mUser.email)
                    }
                })
    }
}



编辑3

override fun onDataChange(p0: DataSnapshot) {
     if (p0.hasChildren()) {
          //Declare mUser as chosen DB User
          tv_main_name.setText(mUser.name)
          tv_main_email.setText(mUser.email)
          }                        
     }

2 个答案:

答案 0 :(得分:0)

    mDatabase.child("users").child(mAuth.currentUser!!.uid).addListenerForSingleValueEvent(object : ValueEventListener {
                    override fun onCancelled(p0: DatabaseError) {
                       //handle error
                    }

                    override fun onDataChange(p0: DataSnapshot) {
                        if (p0.hasChildren()) {
//If you have model for your user data object then do it like this.
                                                  val user = p0.getValue(YourModel::class.java)

                    }

                })

这就是从 users 节点

获取当前登录用户数据所需要做的一切

答案 1 :(得分:0)

工作解决方案

class MainActivity : AppCompatActivity() {

    private lateinit var mUser: User
    private lateinit var mAuth : FirebaseAuth
    private lateinit var mDatabase: DatabaseReference

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

        mAuth = FirebaseAuth.getInstance()
        mDatabase = FirebaseDatabase.getInstance().reference

        mDatabase.child("Users").child(mAuth.currentUser!!.uid)
             .addListenerForSingleValueEvent(object : ValueEventListener {

                    override fun onCancelled(p0: DatabaseError) {
                        TODO("Not yet implemented")
                    }

                    override fun onDataChange(p0: DataSnapshot) {
                        if (p0.hasChildren()) {
                            val user = p0.getValue(User::class.java)
                            tv_main_name.text = user?.name
                            tv_main_email.text = user!!.email
                        }
                    }
                })
    }
}