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
2
-
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. QuinteroMay 28, 2018 at 19:22
-
1OP can you please put code blocks into your question, and not images of code?– dwjohnstonApr 12, 2019 at 1:56
Add a comment
|
1 Answer
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
})
-
I tried add method also, Even tried your code, But didnt worked. It is showing an error Feb 20, 2018 at 2: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. Feb 20, 2018 at 7:25
-
i have edited the question, i have added the picture wihch shows the error, please check it once sir. Feb 20, 2018 at 17:45
-
-
Yes. I just updated my answer. Your error was till showing you using
set
, notadd
. I'm glad that you have it sorted now Feb 20, 2018 at 17:59