简体   繁体   English

如何检查云 firebase 中的唯一别名?

[英]How to check unique alias in cloud firebase?

i want to check nick name in hashmap but i didn't get all document from cloud firebase?我想查看 hashmap 中的昵称,但我没有从云 firebase 中获取所有文档? How can i do that?我怎样才能做到这一点? Thanks in advance.提前致谢。

val db = Firebase.firestore
val docRef = db.collection("User").document()
docRef.get().addOnSuccessListener { documents->
    
    if (documents != null) {

        val obj= documents.get("Basic Information") as HashMap<*, *>
        val checkNick= obj["nick"].toString()

        if (checkNick == nick){
            Toast.makeText(activity, "exists.", Toast.LENGTH_LONG).show()
        }

    } else {
        Log.d(TAG, "No such document")
    }
}

This line of code creates a reference to a new, non-existing document:这行代码创建了对一个新的、不存在的文档的引用:

val docRef = db.collection("User").document()

Since the document doesn't exist yet, calling docRef.get() leads to an empty snapshot.由于文档尚不存在,因此调用docRef.get()会生成一个空快照。


If you want to load a specific document, you'll need to know its document ID and specify that in the call to document :如果要加载特定文档,则需要知道其文档 ID 并在调用document时指定:

val docRef = db.collection("User").document("documentIdThatYouWantToLoad")

If you want to check whether any document exists in the collection for the given nickname, you can use a query :如果要检查给定昵称的集合中是否存在任何文档,可以 使用查询

val query = db.collection("User").whereEqualTo("nick", nick)

Since a query can get multiple documents , you also need to handle that differently:由于查询可以获取多个文档,因此您还需要以不同的方式处理:

query.get().addOnSuccessListener { documents ->
    if (!documents.empty) {
        Toast.makeText(activity, "exists.", Toast.LENGTH_LONG).show()
    }
    for (document in documents) {
        Log.d(TAG, "${document.id} => ${document.data}")
    }
}
.addOnFailureListener { exception ->
    Log.w(TAG, "Error getting documents: ", exception)
}

Also see:另见:

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何检查 firebase 数据库中是否存在唯一条目? - How to check if unique entry exists in firebase database or not? 如何使用 Google Firebase Cloud Functions 检查位置是否在服务器上的地图范围内 - How to check if locations are within map bounds on server with Google Firebase Cloud Functions 如何调试Firebase云消息? - How to debug Firebase Cloud Messaging? 如何为 Firebase 中的每个集合创建唯一 ID? - How to create unique id for each collection in Firebase? 如何在Firebase中将唯一ID保存为图像名称? - How to save unique id as a name of a image in Firebase? 如何将 Google Cloud 凭据导入 Firebase Cloud Function? - How to import Google Cloud credentials into a Firebase Cloud Function? 如何使用云功能以使用 Firebase 云视觉 - How to use the Cloud Functions in order to use Firebase Cloud vision Firebase 在手机上如何验证对云 function 的调用? - Firebase on mobile how to authenticate call to cloud function? 如何为 firebase 云功能设置 vpc 连接器? - How to setup vpc connector for firebase cloud functions? Firebase:如何在 onCreate 中获取附加用户信息 Firebase 云 function - Firebase: how to get getAdditionalUserInfo in onCreate Firebase Cloud function
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM