skip to Main Content

I have manually added a document to a Firestore collection, and as its ID, I copied/pasted the ID of the current user, and to that document, I have added one field and one corresponding value of data.

After this, I attempted to retrieve the document I added, but unsuccessfully, and the very first stumbling point was that Firestore returned that the document did not exist.

At this question, my interest is in how I can get Firestore to return "true" regarding the existence of this document.

final _grocerData = FirebaseFirestore.instance.collection('grocers/{abc1}/fruits/{abc2}/apples/');

The collection reference is defined as above.

enter image description here

The image is to confirm the existence of the document.

enter image description here

For the avoidance of any doubt, the full document-collection reference.

Future<void> getData() async {
  final document = await _grocerData.doc(_auth.currentUser!.uid).get();
  if (document.exists) {
    print('Hooray!  The document was found!');
  } else {
    print('Boohoo!  Firestore cannot find the specified document');
    print('The current-user's ID: ${_auth.currentUser!.uid}');
  }
}
I/flutter (20975): Boohoo!  Firestore cannot find the specified document
I/flutter (20975): The current-user's ID: cGr1Fh8d9PgI9VJ2UzbeBVmRbhk2

The method contains a reference that points specifically at the document that has been created (the document’s ID is the current user’s ID).

Despite these things, Firestore declares that the document does not exist by choosing the if-route associated with if-does-not-exist.

So my question is why, and how can this rectified?

2

Answers


  1. Chosen as BEST ANSWER

    For this question I added the document manually, but when I tried adding the document automatically via Firestore's set method, when I enquired for whether the document existed, Firestore confirmed that the document did exist.

    It says to me overall, that Firestore does not count documents which were added manually. This view may be correct or incorrect, but for some reason, after adding the document by a Firestore method, that is when the document was found.

    final _fruitData = FirebaseFirestore.instance.collection('grocers/{abc1}/fruits');

    I used an alternative collection reference during this finding.

    Future<void> setData2() async {
        try {
    
          final docIntent = await _fruitData.doc(_auth.currentUser!.uid).set({
            'stackExample': '15 February',
          });
    
          // Check if document exists after setting data
          final documentSnapshot = await _fruitData.doc(_auth.currentUser!.uid).get();
          if (documentSnapshot.exists) {
            print('Document created successfully!');
          } else {
            print('Error: Document creation failed.');
            // Handle this error appropriately (log, display message)
          }
        } on Exception catch (e) {
          // Handle errors more robustly (log, display user-friendly message)
          print('Error setting data: $e');
        }
      }

    I used an alternative method when making this finding, but the method features the same call checking for whether or not the document exists, and the response was the positive if-route.

    So, finally, here are two routes for resolving why Firestore can report that an existing document does not exist.

    This issue is important because if Firestore cannot find a document, then Firestore cannot assist with retrieving data regarding that sought document.

    Now that this issue is resolved, maybe now I can retrieve the data I am seeking from a Firestore document - and you too : )


  2. You cannot read the document because you’re using an incorrect document reference:

    final _grocerData = FirebaseFirestore.instance.collection('grocers/{abc1}/fruits/{abc2}/apples/');
    

    You cannot create a reference using wild cards. So to be able to create a reference to a document that exists inside a sub-sub-collection, then you need to know the names of all collections and documents ahead of time. So your reference should look something like this:

    final _grocerData = FirebaseFirestore.instance
                                         .collection("grocers")
                                         .doc("FhLH...X7us")
                                         .collection("fruits")
                                         .doc("alWS...2xy6")
                                         .collection("apples")
                                         .doc("cGr1...bhk2");
    

    See, inside this document reference object, I have specified the names of all collections and the IDs of all documents as I’ve seen in your screenshot. Now you can read the document and get the desired result:

    Hooray! The document was found!
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search