there is no documentation about how add sub collection in a document in firestore, how to add a sub collection by using a web app? even tried like this but didn't worked. how can i add a sub collection in apps by using code? is there any way to do it or it will be better to use firebase real time database? if it so, please let me know how to do it in firebase as welli want to add like this i did this but iam getting an error like this the error says this
Asked
Active
Viewed 7.0k times
58
-
2The solution goes: Get the interested document (doc) and then: doc.ref.collection('Collection_Name').add('Object_to_be_added') It is advisable to include a then/catch after the promise. – Santiago M. Quintero May 28 '18 at 19:22
-
1OP can you please put code blocks into your question, and not images of code? – dwjohnston Apr 12 '19 at 01:56
1 Answers
84
Your code should work if you change .set
to .add
. Cloud Firestore will then issue a new ID for that document. If you want to specify the document ID, then you'll need to do this...
Set a book with a given ID
db.collection('users').doc(this.username).collection('booksList').doc(myBookId).set({
password: this.password,
name: this.name,
rollno: this.rollno
})
This page details how to Add data to Cloud Firestore
Add a book
db.collection('users').doc(this.username).collection('booksList').add({
password: this.password,
name: this.name,
rollno: this.rollno
})

Jason Berryman
- 4,760
- 1
- 26
- 42
-
I tried add method also, Even tried your code, But didnt worked. It is showing an error – user8167002 Feb 20 '18 at 02:52
-
1You will need to paste the error here. You have provided so little code in your screenshot, it is impossible to be sure what the issue is. The normal practice is to paste code snippets into Stack Overflow, not simply provide screenshots. – Jason Berryman Feb 20 '18 at 07:25
-
i have edited the question, i have added the picture wihch shows the error, please check it once sir. – user8167002 Feb 20 '18 at 17:45
-
-
Yes. I just updated my answer. Your error was till showing you using `set`, not `add`. I'm glad that you have it sorted now – Jason Berryman Feb 20 '18 at 17:59
-
If this is working now, please mark this post as answered. There is a tick on the left – Jason Berryman Feb 20 '18 at 18:03