I'm building a story editor where you can write stories and separate them into different chapters and I'm having trouble accessing my subcollection for NextJS's "getStaticPaths" because every document is a UUID.
I have data stored in a Firebase Firestore like this:
stories
├── UUID1 (Story 1)
│ └──chapters
│ └── UUID (Chapter 1)
│ └── UUID (Chapter 2)
│
│
├── UUID2 (Story 1)
│ └──chapters
│ └── UUID (Chapter 1)
│ └── UUID (Chapter 2)
│
Problem
I can't access the chapters subcollection because I can't fetch the UUIDs.
Normally, to access the chapters, I create a useEffect hook to store the UUID into a useState hook so I can send a query like the code below:
const chaptersQ = query(
collection(db, "stories", story.data().id, "chapters")
);
Unfortunately, in trying to create dynamic routing and "getStaticPaths" exist before/outside the main function, so I can't use react hooks.
Here's my current code:
export const getStaticPaths = async () => {
const storyQ = collection(db, "stories");
const queryStory = await getDocs(storyQ);
queryStory.forEach(async (story) => {
const chaptersQ = query(
collection(db, "stories", story.data().id, "chapters")
);
const queryChapters = getDocs(chaptersQ);
return {
paths: (await queryChapters).docs?.map((doc) => ({
params: {
id: `${doc.data().storyID}`,
chapter: `${doc.data().chapter}`,
},
})),
};
});
};
This returns an invalid error, as the paths somehow receive an undefined.
I've spent hours working on this. The closest I've gotten is by declaring a variable with "let" in the root layer of the file and then pushing chapter doc.data into it.
Then using that in the return statement to set the paths and parameters. However, this also returned and undefined error.
Thank you in advance!