Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I get specific document data from firestore querysnapshot?

I got a querysnapshot in a function. And want to bring the whole querysnapshot to another function (functionTwo). In functionTwo, I want to get a specific document in the querysnapshot WITHOUT forEach. The specific doc can be changed by different cases.

ref_serial_setting.get()
    .then(querysnapshot => {
      return functionTwo(querysnapshot)
    })
    .catch(err => {
      console.log('Error getting documents', err)
    })


let functionTwo = (querysnapshot) => {
  // getting value

  const dataKey_1 = "dataKey_1"

  // Tried 1
  const value = querysnapshot.doc(dataKey_1).data()

  // Tried 2
  const value = querysnapshot.document(dataKey_1).data()

  // Tried 3 (Put 'data_name': dataKey_1 in that doc)
  const value = querysnapshot.where('data_name', '==', dataKey_1).data()
}

The result are all these trying are not a function.

How can I get specific document data from querysnapshot??

or

Is there any easy method to change the querysnapshot to JSON?

like image 284
Mike Yan Avatar asked Jun 05 '18 04:06

Mike Yan


People also ask

How do I get a random document in a collection firestore?

An easy way to grab random documents is get all the posts keys into an array ( docA , docB , docC , docD ) then shuffle the array and grab the first three entries, so then the shuffle might return something like docB , docD , docA .

What is QuerySnapshot in firebase?

A QuerySnapshot contains zero or more QueryDocumentSnapshot objects representing the results of a query. The documents can be accessed as an array via the docs property or enumerated using the forEach method. The number of documents can be determined via the empty and size properties.


2 Answers

You can get an array of the document snapshots by using the docs property of a QuerySnapshot. After that you'll have to loop through getting the data of the doc snapshots looking for your doc.

const docSnapshots = querysnapshot.docs;

for (var i in docSnapshots) {
    const doc = docSnapshots[i].data();

    // Check for your document data here and break when you find it
}

Or if you don't actually need the full QuerySnapshot, you can apply the filter using the where function before calling get on the query object:

const dataKey_1 = "dataKey_1";    
const initialQuery = ref_serial_setting;
const filteredQuery = initialQuery.where('data_name', '==', dataKey_1);

filteredQuery.get()
    .then(querySnapshot => {
        // If your data is unique in that document collection, you should
        // get a query snapshot containing only 1 document snapshot here
    })

    .catch(error => {
        // Catch errors
    });
like image 54
Peza Avatar answered Oct 04 '22 07:10

Peza


Theres an easy way to do this, each QuerySnapshot has a property docs which returns an array of QueryDocumentSnapshots. See QuerySnapshot documentation.

let citiesRef = db.collection('cities');
let query = citiesRef.where('capital', '==', true).get().then(snapshot => {
  snapshot.docs[0]; // => returns first document
});
like image 42
dcts Avatar answered Oct 04 '22 07:10

dcts