Reputation: 275
I'm trying to figure out how to construct a new sub-collection (RED CIRCLE) inside the following directory in Firestore:
directory: "courses/{userID}/{courseID} "
with the courseID to be randomly generated ID upon creation of the sub-collection inside the "userID" document. Unfortunately the code I was able to get working, using the .set(), doesn't create a new sub-collection, but rather just updates the current document.
Is there a function that I can use to create a new collection inside the document?
CODE:
export const createCourse = (course) => {
(...)
//firestore.collection("courses").doc(authorId).set({ <---This commented out line updates the document, yet I'm trying to construct a new sub-collection inside this document, not just change fields inside of it.
firestore.collection("courses").doc(authorId).add({ <--- This gives me an error stating " firestore.collection(...).doc(...).add is not a function ". I am trying to figure out what is the correct syntax for this intended action.
...course,
authorUID: authorId,
courseCreatedAt: new Date()
}).then(() => {
(...)
};
Upvotes: 0
Views: 76
Reputation: 317372
There is no API for what you're trying to do. Also, it's generally not a good idea to use random collection name to model your data. Collections names should be known by your app ahead of time, because:
Consider instead using a subcollection named "courses", then add a document under there with a random ID.
Upvotes: 1