Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting a specific field in a document returned from firebase firestore

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:

enter image description here

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);
                    });
like image 445
JWolfCrafter Avatar asked Sep 23 '18 01:09

JWolfCrafter


People also ask

Which function is used to fetch data from the firebase document?

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.

How do I aggregate data in firestore?

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.

What is reference field in firestore?

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 .


1 Answers

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;
like image 182
Doug Stevenson Avatar answered Sep 20 '22 15:09

Doug Stevenson