I have database (Like the image below) and I Need to get the name of subcollection which contain Quiz Name
CodePudding user response:
There are no way to get collections names (well on client side for sure). Collections are like table name in regular databases. If you don't know what you can have in collections, and you let users create their own collections, you will end up having your database used by some hackers. This is how rules should look like in firebase.
rules_version = '2';
service cloud.firestore {
match /databases/{database}/documents {
// This line prevent anyone except server to manipulate database.
// All other lines overwrite this rule and let do something in database for users.
match /{document=**} {
allow read, write: if false;
}
// Here, users can create and update their documents, but they cannot delete them.
// Because of rules above, they cannot create nested collection.
// This is important because someone may do his own nested collection and use it to his project.
match /bookings/{docId} {
allow read: if resource.data.owner == request.auth.uid || isAdmin()
allow update: if resource.data.owner == request.auth.uid || isAdmin()
allow create: if request.auth != null
}
match /app/{docId} {
allow read: if true;
allow write: if false;
}
}
}
function isAdmin() {
return request.auth.token.admin == true;
}
Quiz rules:
rules_version = '2';
service cloud.firestore {
match /databases/{database}/documents {
match /{document=**} {
allow read, write: if false;
}
match /quizzes/{docId} {
allow read: if resource.data.public == true || resource.data.owner == resuest.auth.uid
allow update, detele: if resource.data.owner == request.auth.uid
allow create: if request.resource.data.owner == request.auth.uid
}
}
}
Example query web in TypeScript:
function getQuizzes() {
const ref = collection('quizzes')
const qRef = query(ref, ...[where("level", ">=" 1), where("public", "==", true ), where("category", "==", "math"), order("level", "asc")])
return getDocs(qRef)
}
Code might be buggy. I didn't test it.