Adding a document to a subcollection of a collection in Firestore
See original GitHub issueApologies if I am doing something wrong, but I am not sure if this is an issue with the plugin or not.
I have documents in a “message” collection. I have manually added a “comments” sub-collection to some of those documents through the web ui, and tried the below code on both documents with and without that existing sub-collection added.
The code that I’m having trouble with looks like this:
this.messageRef = firebase.firestore.collection('messages').doc(this.message_id);
this.messageRef.collection("comments").add({
comment: this.commentToAdd,
created_at: new Date()
})
.then(function (docRef) {
// ..
})
.catch(function (error) {
console.error("Error adding document: ", error);
});
I am getting this error:
JS ERROR TypeError: this.messageRef.collection is not a function. (In 'this.messageRef.collection("comments")', 'this.messageRef.collection' is undefined)
Am I using an incorrect object or something? Surprisingly I can find so little help on this issue through my usual google-fu, so maybe this question will help someone else.
Thanks.
Issue Analytics
- State:
- Created 5 years ago
- Comments:6 (4 by maintainers)
Top Results From Across the Web
how to add sub collection to a document in firestore? [closed]
The solution goes: Get the interested document (doc) and then: doc.ref.collection('Collection_Name').add('Object_to_be_added') It is advisable ...
Read more >Cloud Firestore Data model - Firebase
All documents must be stored in collections. Documents can contain subcollections and nested objects, both of which can include primitive fields like ...
Read more >Create a Firestore subcollection reference - Google Cloud
Stay organized with collections Save and categorize content based on your preferences.
Read more >firebase subcollection | The AI Search Engine You Control
' To get all the product documents of a specific Order you would write something like Firebase.firestore().collection("Orders/orderid123/Products").get() where ...
Read more >Creating Subcollections - FlutterFlow Docs
Click on the Firestore from the Navigation Menu (left side of your screen). · Click on the (+) Plus sign button. · A...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Yes! You are right, this works perfectly both for adding to a subcollection and getting the documents in a subcollection. I have to admit that I didn’t know that there was a distinction between the Web API and whatever I was using. What was I using, and any direction on when to use one vs the other?
For the record, as you know, I was incorrectly doing it like this:
Whereas it needed to be
Then I was able to chain a collection onto it.
THANK YOU.
Awesome, glad you got it working!
So this plugin started out with a proprietary API that I came up with to unify the iOS and Android SDK implementations. Later, I realized there’s also a Web API. To support code sharing between web and native (or easy migration from web to native) I decided to add a new API that has the same interface as the Firebase Web SDK. I didn’t want to remove/deprecate the old API because it would harm existing users, so that’s why the Web API needs a different import.