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.

Firestore Add Sub-Collection Example

See original GitHub issue

Well, I’ve been at it a week or too, and I’m just too green to figure out how to reference a sub-collection from within a template (if that’s even possible), or what the best way to get this data would be.

What Im attempting to accomplish is loop through a collection of tickets, in which each ticket has a sub-collection of comments. I’d want to display all comments, for each ticket in the list.

Heirarchy goes: DB > Tickets Collection > ticketdocs > Comments Collection > commentdocs

app.js:

const List = {
  template: require('./list.html'),
  // template: '<div>Ticket List!</div>',
  data: function () {
    return {
      tickets: [],
    }
  },
  firestore() {
    return {
      tickets:     db.collection('tickets'),
    }
  },
}

list.html.js:

<div v-for="ticket in tickets">
  <p>{{ ticket.comments }}
</div>

Displaying/iterating over {{ ticket }} works great and binds up fine, but accessing the sub-collection is where I’m stumped.

I’m sorry for such a basic request with poor descriptions/examples, but with the new Firestore sub-collections, Im sure some folks would love some examples of how to get access to sub-collections in the various ways available.

Thank you!

Issue Analytics

  • State:open
  • Created 6 years ago
  • Reactions:8
  • Comments:13 (6 by maintainers)

github_iconTop GitHub Comments

12reactions
SumNeuroncommented, Feb 28, 2020

can you please provide a clean example? I am struggling

8reactions
posvacommented, Jan 18, 2018

I don’t think is such a basic request, it’s definitely something worth looking at not easy to add in a declarative way. Unfortunately, after some research, I couldn’t find a way of reading the collections from a document (in the web sdk) (https://cloud.google.com/firestore/docs/query-data/get-data). Maybe I can add some API to allow the user bind collections on the document:

this.$bind('collection', collectionRef, { onDocumentBind })
function onDocumentBind(doc, bind) {
	// bind is bound to doc
	bind('comments', db.collection(`collection/${doc.id}/comments`), { onDocumentBind: () => {} })
}

this would allow me to automatically unbind the collection when document is unbound. But I still need to check if it’s feasible 🙂

Read more comments on GitHub >

github_iconTop Results From Across the Web

Create a Firestore subcollection reference - Google Cloud
Stay organized with collections Save and categorize content based on your preferences.
Read more >
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
Subcollections allow you to structure data hierarchically, making data easier to access. To get all messages in roomA , you can create a...
Read more >
Creating Subcollections - FlutterFlow Docs
2. Creating a subcollection · Click on the Firestore from the Navigation Menu (left side of your screen). · Click on the (+)...
Read more >
Adding a document to a subcollection of a collection in Firestore
Hi, if you search for .doc here, you'll see a lot of examples (use .get on the doc reference, not collection ).
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