[英]Firebase get value from one field when searching for different field
As of right now I am using firebase to get a user in a "users" collection like this:截至目前,我正在使用 firebase 在“用户”集合中获取用户,如下所示:
val query: Query = usersCol.whereEqualTo(USERNAME_KEY, usernameInput)
USERNAME_KEY is a string "username" which is the same as in the database USERNAME_KEY是一个字符串“用户名”,与数据库中的相同
usernameInput is the username of the user I'm getting from the database usernameInput是我从数据库中获取的用户的用户名
(The primary key is an id that I can't search for) Is it possible to get the value this primary key? (主键是一个id,我查不到)这个主键能取值吗? or other fields in the same "level" as username, like 'name' or 'password'
或与用户名处于同一“级别”的其他字段,例如“名称”或“密码”
I'm imagining something like:我在想象这样的事情:
query.get("id")
but that doesn't work...但这不起作用...
You will need to execute the query before you can get results from it.您需要先执行查询,然后才能从中获得结果。 As shown in the Firebase documentation you can execute a query like this:
如 Firebase 文档中所示,您可以 执行如下查询:
query
.get()
.addOnSuccessListener { documents ->
for (document in documents) {
Log.d(TAG, "${document.id} => ${document.data}")
}
}
.addOnFailureListener { exception ->
Log.w(TAG, "Error getting documents: ", exception)
}
Within that loop, you can get a specific field from the document
with:在该循环中,您可以使用以下命令从
document
中获取特定字段:
document.get("name")
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.