简体   繁体   English

Firebase 搜索不同字段时从一个字段获取值

[英]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.

相关问题 Flutter Firebase 遍历匹配字段值的所有文档,并将每个文档的字段插入字符串列表 - Flutter Firebase Iterate through all documents that matches field value and inserting field from each one into a List of String 从 firebase flutter 获取字段和值 - Get field and values from firebase flutter 如何在 Firebase Firestore 上获取一个字段? - How can I get one field on Firebase Firestore? 从 Firestore 中的一个文档中获取一个字段 - Get one field from one document in Firestore 从 BigQuery 上的字符串字段获取结构化值 - Get a structured value from string field on BigQuery 如何获取 firebase firestore android 中某些字段的值? - How to get value of some field in firebase firestore android? 如何从一个用户的 firebase 实时数据库到 flutter 中的文本字段获取当前用户数据,如姓名和 email? - How to get current user data like name and email from firebase realtime databse for one user to Text Field in flutter? Firebase 安全规则:当文档具有特定字段时允许“获取” - Firebase security rules: allow 'get' when document has a specific field Firebase 按特定字段值查询 - Firebase query by specific field value 如何在一个字段中获取 flutter firebase 中的卖家 ID 和产品 ID,例如产品字段 - how to get seller id and product id in flutter firebase in one field for example, product field
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM