So, I have a problem that can probably be super easily solved I just can't quite figure it out. Essentially at this point, I'm trying to store fields of a specific document into 2 vars so that I can use them elsewhere.
This is my firestore hierarchy:
This is the code I have so far and I think I'm on the right track but I don't know what to replace "//What do I put here" with.
var db = firebase.firestore();
var user = firebase.auth().currentUser;
var usersEmail = user.email;
db.collection("users").where("email", "==", usersEmail)
.get()
.then(function(querySnapshot) {
querySnapshot.forEach(function(doc) {
// doc.data() is never undefined for query doc snapshots
console.log(doc.id, " => ", doc.data());
var firstName = //What do I put here?
var lastName = //What do I put here?
});
})
.catch(function(error) {
console.log("Error getting documents: ", error);
});
The Get() function in Go unmarshals the data into a given data structure. Notice that we used the value event type in the example above, which reads the entire contents of a Firebase database reference, even if only one piece of data changed.
If you want to gain insight into properties of the collection as a whole, you will need aggregation over a collection. Cloud Firestore does not support native aggregation queries. However, you can use client-side transactions or Cloud Functions to easily maintain aggregate information about your data.
REFERENCE DATA TYPE IS ONE OF THE MANY DATA TYPES OF CLOUD FIRESTORE . THIS DATA TYPE ACTS LIKE FOREIGNKEY EVEN THOUGH IT IS NOT ACTUALLY A FOREIGNKEY . THE MAIN ADVANTAGE OF USING IT IS TO REFER A CERTAIN DOCUMENT TO ANOTHER DOCUMENT .
doc.data()
is just a regular JavaScript object with the contents of the document you just read:
var data = doc.data();
var firstName = data.first;
var lastName = data.last;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With