Reputation: 17
I've got a Firestore database in Firebase for my Angular web app. Now I'm trying to create new documents within my collection "users". I know how to create those but only with simple fields ("mail, nickname and userID"). I also want to add a subcollection "routes" to this document but I don't know how. There will be documents within this subcollection.
Here is a photo of my DB in Firebase: https://i.sstatic.net/3nZMn.png
This is the function I use to create a new user in this database. It is working but the subcollection is missing.
create_new_User(docID, email, nickname, userID) {
return this.fireservices.collection('users').doc(docID).set({
mail: email,
nickname: nickname,
userID: userID
});
}
Has someone any ideas to fix this problem?
Upvotes: 0
Views: 938
Reputation: 189
I would personally make the docId for user collection to be the same as the actual userId but you don't have to do it this way. You can only create one document at a time so you need to either batch this or run them in 2 seperate document writes. You can do something like this for example with out a batch:
create_new_User(userId, email, nickname, routeId, routeParams) {
this.fireservices.collection('users').doc(userId).set({
mail: email,
nickname: nickname,
userID: userID
})
this.fireservices.collection('users').doc(userId).collection('routes').doc(routeId).set({ routeParams })
}
Upvotes: 2