首页 > 解决方案 > 无法从 Firestore 读取数据

问题描述

这是 Firestore 数据库

我正在尝试使用电话号码注册用户,我的目标是用户只能使用一个电话号码注册意味着没有重复,所以在注册时我正在 Firestore 中读取,如果输入的号码在数据库中可用,则显示错误,否则它将注册。但是在读取数据 Firestore 时,什么也没有返回。它也没有显示任何错误吐司。相同的功能在其他应用程序中运行良好。

这是代码

 mFireStore = FirebaseFirestore.getInstance()
    mFireStore.collection("USERS")
        .whereEqualTo("Number", number.text.toString())
        .get()
        .addOnSuccessListener { documents ->
            for (document in documents) {

                if( document != null ){

                    Log.i("Null","Document Not null")

                    Toast.makeText(this,"Phone Number Already Exist",Toast.LENGTH_SHORT).show()

                }else{

                    val OTP = Intent(this@MainActivity,OTPActivity::class.java)
                    OTP.putExtra("Name",NAME.text.toString())
                    OTP.putExtra("Number","+"+92+number.text.toString())
                    OTP.putExtra("Age",age.text.toString())
                    OTP.putExtra("District",district.text.toString())
                    startActivity(OTP)
                }
            }
        }
        .addOnFailureListener { exception ->
            Toast.makeText(this,exception.toString(),Toast.LENGTH_SHORT).show()
        }

这是我的安全规则

match /{document=**} {
   allow read, write: if true;
}

标签: androidfirebasekotlingoogle-cloud-firestore

解决方案


如果要查询 Firestore 数据库,需要先创建索引。通过这个链接,它会达到你的目的,希望!


推荐阅读