What is the role of doc.data() in Firebase?

Viewed 2403

I'm building a simple page where I want to render all items from FireStore documents. However, I do not understand what exactly FireStore provides. Is it a promise, object or something different?

Standard functon uses doc.data() to get the information. Can somebody explain why it happens? What exactly doc.data() does?

db.collection("cities").get().then(function(querySnapshot) {
    querySnapshot.forEach(function(doc) {
        // doc.data() is never undefined for query doc snapshots
        console.log(doc.id, " => ", doc.data());
    });
});
1 Answers

But why it has to be with console.log(doc.id, " => ", doc.data());

This is just a way to print, with console.log(), the id and field values (through data()) of each Firestore DocumentSnapshot. As explained here, console.log() can take as parameters "a list of JavaScript objects to output". "The string representations of each of these objects are appended together in the order listed and output".


Now to solve your error, you should do as follows (untested):

db.collection('teamMember').get()
.then(querySnapshot => {
   querySnapshot.docs.map(doc => { return <TeamCard name={doc.Name} position={doc.position} text={doc.text} src={doc.url} /> });
});

because the map() method is a method of an Array, and a Firestore querySnapshot is not an array. By calling the docs method, you get an Array, that you can use with map().

Related