Within my Firestore database, I have a structure of users that looks like this:
https://i.sstatic.net/jgeCq.png
The rules set up for this database are as follows:
match /users/{userID} {
match /public {
allow read: if request.auth != null;
}
match /private/{document=**} {
allow read: if request.auth.uid == userID;
}
}
I am facing challenges in fetching only the public data from the user collection without retrieving everything and running into issues due to these rules. I am utilizing the Firebase Web Modular API, but it seems that the earlier version includes a .select function. However, I am unsure about its compatibility with my current code.
The latest attempt at implementing this functionality, which was auto-generated by ChatGPT and is not working properly, may seem confusing, but it represents my closest approach to solving the issue.
export const getUserFromHashedUID = async (hashedUID, fields) => {
const db = getFirestore();
// const userDocRef = doc(collection(db, 'users', hashedUID), 'public');
const userPublicCollectionRef = collection(db, 'users', hashedUID, 'public');
const userDocRef = doc(userPublicCollectionRef, 'public');
try {
const docSnapshot = await getDoc(userDocRef);
if (docSnapshot.exists()) {
const publicData = fields.reduce((acc, field) => {
acc[field] = docSnapshot.get(field);
return acc;
}, {});
console.log(publicData);
} else {
console.log('No such document!');
}
} catch (error) {
console.error('Error getting document:', error);
}
};
Answer: Upon receiving feedback, it appears that the mentioned feature is not available in the JavaScript SDK for some reason. Therefore, I'm considering restructuring my database layout as shown below to address this limitation:
users:{
"1234567-private":{data:"data"},
"1234567-public":{data:"data"},
"9876543-private":{data:"data"},
"9876543-public":{data:"data"},
}
This new arrangement provides similar efficiency to the original setup but should work seamlessly with rules and other functions (with some customization).