skip to Main Content

I have pasted the user-id of the current Firebase user as the document-id of a Firestore document, and I have given that document a single field and data.

Retrieving the data from Firestore is the problem I have, as I cannot find the way to draft a get query.

I have tried two ways and both of these return that the document has not been found.

The security rules allow read on the relevant collection.

final _grocerData = FirebaseFirestore.instance.collection('/grocers/{abc1}/fruits/{abc2}/apples');
  Future<void> getDetail() async {
    try {
      final document = _grocerData.doc(_auth.currentUser!.uid);
      final docSnapshot = await document.get();
      if (docSnapshot.exists) {
        final data = docSnapshot.data();
        String grocerID = data?['grocerID'];
        print('Firestore-Field was $grocerID');
      } else {
        print('Result = Grocer ID was not retrieved.');
        print('Current user's ID = ${_auth.currentUser!.uid}');
        return null;
      }
    } catch (e) {
      print(e);
    }
  }
  Future<void> getDetail2() async {
    final document = _grocerData.doc(_auth.currentUser!.uid);
    document.get().then(
          (doc) {
        if (doc.exists) {
          String grocerID = doc.data()?['grocerID'];
          print('Data retrieved: $grocerID');
        } else {
          print('Result = Grocer ID was not retrieved.');
          print('Current user's ID = ${_auth.currentUser!.uid}');
        }
      },
      onError: (e) {
        print('Error getting document: $e');
      },
    );
  }

These methods always return the result where the relevant document doesn’t exist, whereas it does exist, so I am confused as what the code is for retrieving a document.

Can you assist with resolving this problem?

2

Answers


  1. i am not sure wahts are your intension are when you say "How is Firestore’s "get" function correctly coded in order to retrieve a single field of data within a Firestore document?"

    please clarify it

    are you want to know how to query just a single field with in the document and don’t want to pull all of the document ?

    if so then : you can’t pull just a single field you have to query the whole document and from there you extract what you are interested in .how ever for server side work you can use Query.html#select .also go through read is-there-a-way-to-select-only-certain-fields-from-firestore

    you use Firestore Node.JS client or Firebase Admin SDK, then you can
    use select() to select fields:

    but if you are intrested in a document fied value and understand the how the document are being query.then look at following flocks might be helpful.

        Future<void> getDetail2() async {
          DocumentSnapshot snapshot =
              await FirebaseFirestore.instance.collection('collectionName').doc(documentId).get();
     print('${snapshot.id} => ${snapshot.data()}'); 
         Map<String, dynamic> data = snapshot.data();
           String grocerID = data?['grocerID']; //use data as your logic flow
          return data[fieldName];
        }
    

    fore better understanding go through get-data#dart_1

    Login or Signup to reply.
  2. How is Firestore’s "get" function correctly coded to retrieve a single field of data within a Firestore document?

    When you call get() on a document, you read/download the entire content of the document along with all the fields that exist inside it. You can indeed select from the snapshot object only the fields you are interested in, but that doesn’t mean that you aren’t downloading the entire document. So there is no way you can select which fields to read from within the document using the web or mobile SDKs. There are, however, two solutions for this situation.

    The first one would be to use denormalization. This means that you should copy the fields that you are interested in, in another document, in a new collection, and then query that collection instead of the original one. In this way, you’ll get documents that will contain only the important fields rather than all fields of the document. So you’ll save a lot of bandwidth and resources. If you’re new to NoSQL databases, this might sound some kind of weird, but it’s actually a quite common practice.

    The second solution, as also @Rainysidewalks mentioned in his answer, is to use the Firestore Node.js client or Firebase Admin SDK. In this way you’ll be able to use select() as in the below example:

    import { Firestore } from "@google-cloud/firestore";
    
    const firestore = new Firestore();
    
    const snap = firestore
      .collection("yourCollRef")
      .select("grocerID", "OtherField") //👈
      .get();
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search