问题
I am developing a forum application. There are two collections related to user information and topic information in my Firebase database.
In the forumTopics
collection, I keep the data on each forum topic.
When I make a Firebase query, I want to get the data of this topic along with the data of the user corresponding to the topicAuthorUID value.
For example, I want to extract the name, avatar information from the document of the user whose topicAuthorUID value is XXX in the userData
collection.
The query I use to get topics:
Future getForumTopics() async {
final result =
await Firestore.instance.collection('forumTopics').getDocuments();
return result.documents;
}
But with this query, I cannot get the name and avatar of the topic's owner.
In summary, I want to print the subject information and the user's name and avatar information.
What query do I need to do to do this? Please help me. Thank you.
回答1:
When I make a Firebase query, I want to get the data of this topic along with the data of the user corresponding to the topicAuthorUID value.
There is no way you can achieve that in a single go. Queries in Firestore are shallow, can only get elements from the collection that the query is run against. There is no way to get documents from two collections in a single query unless you are using collection group query, but it's not the case since both collections in your project have different names.
For example, I want to extract the name, avatar information from the document of the user whose topicAuthorUID value is XXX in the
userData
collection.
So to solve this, two separate queries are required. The first one would be to get the forum topic. Once you have that document, based on the topicAuthorUID
property, you should perform the second query to get the user's name and avatar.
What query do I need to do to do this?
There is actually not a single query, but two. And to answer your question:
Flutter Firebase How to make Sequential Query
There is no way to make a sequential query in the way you say. Two queries are needed.
来源:https://stackoverflow.com/questions/59911661/flutter-firebase-how-to-make-sequential-query