英文:
Why does `collection.get()` return empty results although I do have data? in Firebase/Flutter
问题
I have the following Firestore structure:
attendance (集合)
│
└─── 11-12-2076 (文档)
│
└─── students (集合)
│
└─── d8281c00-fd7d-11ed-9234-3fbd868d12b9 (文档)
And as a path:
/attendance/5-29-2023/students/afe9e3f0-fd77-11ed-a0fc-596eeda998cb
And as an image:
And I'm trying to fetch all the documents under the attendance
collection. Hence, I'm using
.collection('attendance').get()
in order to receive all the documents/subcollections under attendance
.
however, if I print out the docs
, it always returns an empty list []
. Here is my code:
final attendanceCollection = FirebaseFirestore.instance.collection('attendance');
final QuerySnapshot attendanceSnapshot = await attendanceCollection.get();
print('attendanceSnapshot length is: ${attendanceSnapshot.docs.length}');
But the above output is always 0:
attendanceSnapshot length is: 0
What am I doing wrong? Why isn't the collection returning any data?
Essentially, what I'm trying to do is query all the documents:
Future<void> exportAttendanceData() async {
final attendanceCollection =
FirebaseFirestore.instance.collection('attendance');
final QuerySnapshot attendanceSnapshot = await attendanceCollection.get();
for (final attendanceDoc in attendanceSnapshot.docs) {
final studentsCollection =
attendanceCollection.doc(attendanceDoc.id).collection('students');
final QuerySnapshot studentsSnapshot = await studentsCollection.get();
for (final studentDoc in studentsSnapshot.docs) {
print(studentDoc.data()); // Print the data of each student document
}
}
}
Here are my Firestore Security rules (it's not blocking anything):
rules_version = '2';
service cloud.firestore {
match /databases/{database}/documents {
match /{document=**} {
allow read, write: if
request.time < timestamp.date(2023, 6, 24);
}
}
}
What I tried:
- Checked the security rules.
- Checked for any typos with the collection name.
- Made sure
flutter doctor -v
returns no errors/warnings.
Question:
- How can I get all the documents/collections under the
attendance
collection? Why is.collection('attendance').get()
returning an empty array?
英文:
I have the following Firestore structure:
attendance (collection)
│
└─── 11-12-2076 (document)
│
└─── students (collection)
│
└─── d8281c00-fd7d-11ed-9234-3fbd868d12b9 (document)
And as a path:
/attendance/5-29-2023/students/afe9e3f0-fd77-11ed-a0fc-596eeda998cb
And as an image:
And I'm trying to fetch all the documents under the attendance
collection. Hence, I'm using
.collection('attendance').get()
in order to receive all the documents/subcollections under attendance
.
however, if I print out the docs
, it always returns an empty list []
. Here is my code:
final attendanceCollection = FirebaseFirestore.instance.collection('attendance');
final QuerySnapshot attendanceSnapshot = await attendanceCollection.get();
print('attendanceSnapshot length is: ${attendanceSnapshot.docs.length}');
But the above output is always 0:
attendanceSnapshot length is: 0
What am I doing wrong? Why isn't the collection returning any data?
Essentially, what I'm trying to do is query all the documetns:
Future<void> exportAttendanceData() async {
final attendanceCollection =
FirebaseFirestore.instance.collection('attendance');
final QuerySnapshot attendanceSnapshot = await attendanceCollection.get();
for (final attendanceDoc in attendanceSnapshot.docs) {
final studentsCollection =
attendanceCollection.doc(attendanceDoc.id).collection('students');
final QuerySnapshot studentsSnapshot = await studentsCollection.get();
for (final studentDoc in studentsSnapshot.docs) {
print(studentDoc.data()); // Print the data of each student document
}
}
}
Here are my Firestore Security rules (it's not blocking anything):
rules_version = '2';
service cloud.firestore {
match /databases/{database}/documents {
match /{document=**} {
allow read, write: if
request.time < timestamp.date(2023, 6, 24);
}
}
}
What I tried:
- Checked the security rules.
- Checked for any typos with the collection name.
- Made sure
flutter doctor -v
returns no errors/warnings.
Question
- How can I get all the documents/collections under the
attendance
collection? Why is.collection('attendance').get()
returning an empty array?
答案1
得分: 2
Read operations in Firestore are shallow, so reading from /attendance
will only return documents in that specific collection - not from lower level subcollections.
If you want to read all students, you can use a collection group query to do so.
Also see:
- Stack Overflow - Firestore: Does querying a collection also include their sub-collection?
- Stack Overflow - Firestore query subcollections
- Stack Overflow - Firestore getting collection with and without its sub-collection data
英文:
Read operations in Firestore are shallow, so reading from /attendance
will only return documents in that specific collection - not from lower level subcollections.
If you want to read all students, you can use a collection group query to do so.
Also see:
- https://stackoverflow.com/questions/55632268/firestore-does-querying-a-collection-also-includes-their-sub-collection
- https://stackoverflow.com/questions/46573014/firestore-query-subcollections
- https://stackoverflow.com/questions/52457597/firestore-getting-collection-with-and-without-its-sub-collection-data
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论