Firestore Get Data - tuarua/Firebase-ANE GitHub Wiki
The contents of this page are based on the original Firebase Documentation
There are two ways to retrieve data stored in Cloud Firestore. Either of these methods can be used with documents, collections of documents, or the results of queries:
- Call a method to get the data.
- Set a listener to receive data-change events.
When you set a listener, Cloud Firestore sends your listener an initial snapshot of the data, and then another snapshot each time the document changes.
Example data
To get started, write some data about cities so we can look at different ways to read it back:
var citiesRef:CollectionReference = db.collection("cities");
citiesRef.document("SF").setData({
"name": "San Francisco",
"state": "CA",
"country": "USA",
"capital": false,
"population": 860000
});
citiesRef.document("LA").setData({
"name": "Los Angeles",
"state": "CA",
"country": "USA",
"capital": false,
"population": 3900000
});
citiesRef.document("DC").setData({
"name": "Washington D.C.",
"country": "USA",
"capital": true,
"population": 680000
});
citiesRef.document("TOK").setData({
"name": "Tokyo",
"country": "Japan",
"capital": true,
"population": 9000000
});
citiesRef.document("BJ").setData({
"name": "Beijing",
"country": "China",
"capital": true,
"population": 21500000
});
Get a document
The following example shows how to retrieve the contents of a single document using getDocument():
var docRef:DocumentReference = db.collection("cities").document("SF");
docRef.getDocument(onDocSnapshot);
function onDocSnapshot(document:DocumentSnapshot, error:FirestoreError, realtime:Boolean):void {
if (document.exists){
}
}
Note: If there is no document at the location referenced by docRef, the resulting document will be empty and calling exists on it will return false.
Custom objects
The previous example retrieved the contents of the document as a map, but in some languages it's often more convenient to use a custom object type. In AS3, you defined a City class that you used to define each city. You can turn your document back into a City object:
var docRef:DocumentReference = db.collection("cities").document("SF");
docRef.map(City);
docRef.getDocument(onDocSnapshot);
function onDocSnapshot(document:DocumentSnapshot, error:FirestoreError, realtime:Boolean):void {
if (document.exists){
var city:City = document.data as City;
}
}
Important: Each custom class must have a public constructor that takes no arguments. In addition, the class must include properties, each declared as public.
Get multiple documents from a collection
You can also retrieve multiple documents with one request by querying documents in a collection. For example, you can use where() to query for all of the documents that meet a certain condition, then use getDocuments() to retrieve the results:
db.collection("cities").where("capital", "==", true)
.getDocuments(function (snapshot:QuerySnapshot, error:FirestoreError):void {
if (error) {
trace("Error getting documents: "+error);
return;
}
for each (var document:DocumentSnapshot in snapshot.documents) {
trace(document.id);
}
});
In addition, you can retrieve all documents in a collection by omitting the where() filter entirely:
db.collection("cities").getDocuments(function (snapshot:QuerySnapshot, error:FirestoreError):void {
if (error) {
trace("Error getting documents: "+error);
return;
}
for each (var document:DocumentSnapshot in snapshot.documents) {
trace(document.id);
}
});