22

When i add a document with my own document Id (not auto generated), document Id node is in italics as shown in the screenshot from Firestore console. What is the reason behind this?

My code to add data is

const billingRef = db
      .collection('billing/test/2017/months/11')
      .doc();

  billingRef
      .set({ name: 'ABC' })
      .then(_ => {
        console.log('saved');
      })
      .catch(err => {
        console.log(err);
      });

Above code adds a node successfully, but adds node "test" and "months" in italics.

screenshot 1 enter image description here

screenshot 2 enter image description here screenshot 3 enter image description here

My query yields zero results for such records in firestore, following code. How can I query all the nodes under billing?

db.collection("billing").get().then(function(querySnapshot) {
    console.log(querySnapshot.size) // this is always 0
    querySnapshot.forEach(function(doc) {
        console.log(doc.id, " => ", doc.data());
    });
});
Kumar
  • 449
  • 5
  • 7
  • I don't know the actual cause, but it isn't about the ID being auto-generated or not as far as I can tell. I just added a doc with a self-typed ID, and it showed up non-italic, same as all the other (auto-generated) IDs in the list. – Frank van Puffelen Dec 09 '17 at 18:51
  • Note that you're mixing two separate questions here: 1. Why are some document IDs showing as italic in the console? 2. Why doesn't my code return any documents? I recommend posting each as a separate question, as the answers might have little to do with each other. – Frank van Puffelen Dec 09 '17 at 18:52
  • For question 2: do you have permission to read from `/billing`? Easiest way to detect that is to add `.catch((function(error) { console.error(error); })`. – Frank van Puffelen Dec 09 '17 at 18:53
  • Thanks for looking into this Frank, I believe the questions are related because this happens only for document IDs in italics. I have updated my question with the code to show how I add the records to firestore turns document IDs in italic. I have consistently reproduced this behaviour. – Kumar Dec 09 '17 at 19:24
  • If `test` in `billing/test/2017/months/11` ref does not exists in Firestore, try using the `add()` method to add the doc first before referencing to it. – Amy Dec 14 '17 at 06:26
  • 2
    Nobody knows still what's the preblem? – Vlad Dec 17 '17 at 07:06
  • What I have noticed is that when you set data in your Firestore through code, if you add a Document ("test" and "months") that ONLY contains a Collection ("2017" and "11"), then they will be in italic. The moment you add a Field to "test" and "months" the italic will be removed. My intuition (which may be wrong) is if the Document is "initialised" only with a Collection and no Fields, then it is seen like "null" (italic) (lack of better word), but if a Field is added, and also removed, then it is simply empty (no italic). – Simon Bøgh Dec 29 '17 at 22:38
  • 1
    For **question 1**: https://stackoverflow.com/a/48138953/2162226 - Thanks @Frank – Gene Bo Jun 27 '18 at 00:24
  • You can not access data of null doc. But You can access sub-collection of null doc. As it returns **doc.id** , So you can complete your path to sub-collection – Manish Champaneri Sep 18 '19 at 08:39

2 Answers2

25

Following up on my comment above you will see in the Firestore console that for Documents in italic there is a small text saying "This document does not exist, it will not appear in queries or snapshots", for non-italic it says "This document has no data", so the intuition is that when the Document is created in code without any Fields then it is "null" (a subcollection does not count). If a Field is added and removed, then the Document is simply empty and not null.

Since your query for the Documents under billing are in italic ("null" or does not exist), as the text above states, they will not appear in queries.

The solution would be to either add the document through the Firestore console because here Documents are created as empty, or if in code, add a Field and maybe remove it again if not needed, then the Documents will appear in queries.

Simon Bøgh
  • 801
  • 8
  • 13
5

The problem, I later came to find from this Question's answer for me was creating a sub collection to an empty document. This was my code that was bringing up grayed out documents.

       db.collection('temporal')
            .doc('documentexample')
            .collection("files")
            .add({
              name: "Lorem"
            })
            .catch((error) => {
              console.error("Error adding file: ", error);
            });

In the above code the doc documentexample had no field in it. So the code goes ahead and creates documentexample (it has no fields) then creates a subCollection in it files. This according to firebase just grays out the first document documentexample.

The workaround to this is first create the document add a field in it then create a subcollection to it and on and on... For my use-case, I created a function that creates the document and adds a field to it when the user signs up for the first time

Taio
  • 3,152
  • 11
  • 35
  • 59