Reputation: 1655
Here is the structure of my Firestore DB:
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?
Upvotes: 1
Views: 298
Reputation: 83068
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);
});
Upvotes: 1
Reputation: 78
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
Upvotes: 0