Click here to Skip to main content
15,666,974 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I have a fitness dashboard to show activity of users. However the way the database is structured has made this difficult.{collection/document/subcollection/document}

Currently the dashboard does show each user and all the information (i.e. calories) for the previous workout they logged. However some users can do multiple workouts in a set amount of time i.e. 12 hours and there workout and workout information is stored in a subcollection. What i would like to do is for those users who have a subcollection in there document, i would like to query (by set amount of time ) and read all the data in the sub collection for that particular user and add it together (total calories) and show it in the dashboard.

I am having issues doing this because i can retrieving the data and invoking the data. However no errors.

ps. this is for development, actual database is much larger


function App() {
    const [user, setUser] = useState(null);
    const [workouts, setworkouts] = useState([]);
    const [error, setError] = useState(null);

    useEffect(() => {
        const unsubscribe = firebase
            .onAuthStateChanged((user) => setUser(user));
        return unsubscribe;
    }, []);

    useEffect(() => {
        const unsubscribe = FirestoreService.getworkoutItems(
            (qSnapshot) => {
                const updatedworkouts = => ({
            (error) => setError(true)
        return unsubscribe;

    useEffect(() => {
        const fetchData = async () => {
            try {
                const qSnapshot = await getDocs(collection(db, "workouts"));

                let allDocs = [];
                qSnapshot.forEach((doc) => {
                    allDocs.push({ id:, });
                for (const item of allDocs) {
                    const time = 12;
                    const timeDiff = moment(new Date()).subtract(time, "hours")._d;
                    const qSnap = await getDocs(
                        collection(db, `workouts/${}/Extra`).where(

                    let totalCount = 0;
                    totalCount += qSnap.calories;
                    let allQDocs = [];

                    if (!qSnap.empty) {
                        qSnap.forEach((doc) => {
                    } else {
            } catch (err) {
    }, []);

    return (
                <Header />
                <Divider horizontal section>
                    Fitness Monitor

                {!error && user ? (
                    <Card.Group itemsPerRow="8">
                        {workouts &&
                   => {
                                return <Cards key={} workout={workout} />;
                ) : (
                    <h2> sign in </h2>

export default App;


const firebaseConfig = {


const app = initializeApp(firebaseConfig);
export const db = getFirestore(app);

export const auth = firebase.auth();

const provider = new firebase.auth.GoogleAuthProvider();
provider.setCustomParameters({ prompt: "select_account" });

export const signInWithGoogle = () => auth.signInWithPopup(provider);

export const getworkoutItems = (snapshot, error) => {
    const ref = collection(db, "workouts");
    const query = query(ref);
    return onSnapshot(query, snapshot, error);

[![Structure of database for one workout user][1]][1]

[![Structure of database for a user who has a subcollection of workouts][2]][2]

Structure of database for one workout user


Structure of database for a user who has a subcollection of workouts


What I have tried:

Have tried using type literals
Updated 14-Jul-22 7:04am

1 solution

Bad design leads to bad code. There is no logical reason to create a "subcollection" simply because someone works out more than once in a day.

You have a workout for a give date and time by person x. You want to total by day, week, etc. it's simply a matter of grouping by person, date, etc.

Your current design requires you to "promote" your subcollection through various gyrations.
Share this answer

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900