繁体   English   中英

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

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

我想查看 hashmap 中的昵称,但我没有从云 firebase 中获取所有文档? 我怎样才能做到这一点? 提前致谢。

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")
    }
}

这行代码创建了对一个新的、不存在的文档的引用:

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

由于文档尚不存在,因此调用docRef.get()会生成一个空快照。


如果要加载特定文档,则需要知道其文档 ID 并在调用document时指定:

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

如果要检查给定昵称的集合中是否存在任何文档,可以 使用查询

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

由于查询可以获取多个文档,因此您还需要以不同的方式处理:

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)
}

另见:

暂无
暂无

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM