question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Adding a document to a subcollection of a collection in Firestore

See original GitHub issue

Apologies 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:closed
  • Created 5 years ago
  • Comments:6 (4 by maintainers)

github_iconTop GitHub Comments

2reactions
mmccaffcommented, Mar 7, 2019

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:

import firebase from "nativescript-plugin-firebase";

this.messageRef = firebase.firestore.collection('messages').doc(this.message_id);

this.messageRef.collection("comments").add({ ...

Whereas it needed to be

const firebase = require("nativescript-plugin-firebase/app");

this.messageRef = firebase.firestore().collection('messages').doc(this.message_id);

Then I was able to chain a collection onto it.

THANK YOU.

1reaction
EddyVerbruggencommented, Mar 7, 2019

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.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found