1

Here is the structure of my Firestore DB: structure

I have multiple arrays in the object "reserved items". Each array has more than one element stored in there. How do I retrieve the "returnDate" element from each/every single array that is in the object?

2 Answers 2

1

reserved_items is a field of type Array, and each element of this Array is a map/JS Object. You therefore have to iterate over the JavaScript Array representing this field and use the dot notation. Here is an example with the forEach() method:

  var docRef = db.collection('...').doc('63ufq....');

  docRef
    .get()
    .then((doc) => {
      if (doc.exists) {
        const reserved_items = doc.get('reserved_items');

        reserved_items.forEach(element => console.log(element.returnDate));

      } else {
        // doc.data() will be undefined in this case
        console.log('No such document!');
      }
    })
    .catch((error) => {
      console.log('Error getting document:', error);
    });
Sign up to request clarification or add additional context in comments.

Comments

0

First, load your document from your firestore.

var docRef = db.collection("cities").doc("SF");

docRef.get().then((doc) => {
    if (doc.exists) {
        console.log("Document data:", doc.data());
    } else {
        // doc.data() will be undefined in this case
        console.log("No such document!");
    }
}).catch((error) => {
    console.log("Error getting document:", error);
});

now, in doc.data() is your object, which has the 'reserved_items' as property. Then you can use the map() function to iterate your 'reserved_items' and store the needed data in a variable

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.