title: How do you read data from firestore?
body: <p>I'm trying to learn how to use firestore.</p>
  
  <p>The <a href="https://firebase.google.com/docs/firestore/quickstart?authuser=0" rel="nofollow noreferrer">google documentation</a> says to try this format:</p>
  
  <pre><code>db.collection("users").get().then((querySnapshot) =&gt; {
      querySnapshot.forEach((doc) =&gt; {
          console.log(`${doc.id} =&gt; ${doc.data()}`);
      });
  });
  </code></pre>
  
  <p>So, I used that to do this (changing the db to fsDB and "users" to "newsletters"</p>
  
  <pre><code>fsDB.collection("newsletters").get().then((querySnapshot) =&gt; {
      querySnapshot.forEach((doc) =&gt; {
          console.log(`${doc.id} =&gt; ${doc.data()}`);
      });
  });
  </code></pre>
  
  <p>When I try this, I get an error that says querySnapshot is not defined. Screen shot of error message is attached.</p>
  
  <p><a href="https://i.stack.imgur.com/gixyj.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/gixyj.png" alt="enter image description here"></a></p>
  
  <p>I've seen <a href="https://stackoverflow.com/questions/50154113/unable-to-retrieve-data-from-cloud-firestore-using-queries">this post</a> and tried its suggestion as follows:</p>
  
  <pre><code>fsDB.collection("newsletters").query.get().then(function(querySnapshot) { 
  
                              if (querySnapshot.empty) { 
                                  console.log('no documents found');
                              } else {
                                      querySnapshot.docs.map(function (documentSnapshot) {
                                          console.log(documentSnapshot.data().name); 
                                      });
                              }
  
                          });
  </code></pre>
  
  <p>This attempt generates a parsing error with the if statement, but I can't find a way to solve that either.</p>
  
  <p>This must have a simple solution, but I can't figure out how to get a record from the database.</p>
  
  <p>NEXT ATTEMPT</p>
  
  <p>I tried moving the code above the return statement and putting in inside a variable as follows shown in the attached screen shot, but it still produces errors as shown. These errors are the same regardless of whether i keep or lose the brackets around querySnapshot. </p>
  
  <p><strong>ADD STATE</strong></p>
  
  <pre><code>class Fndate extends React.Component {
      state = {
          options: [],
          anzic: [],
          country: [],
          region: [],
  
      }
  
      selectCountry (val) {
          this.setState({ country: val });
        }
  
        selectRegion (val) {
          this.setState({ region: val });
        }
  
      async componentDidMount() {
          // const fsDB = firebase.firestore(); // Don't worry about this line if it comes from your config.
          let options = [];
          await fsDB.collection("abs_for_codes").get().then(function (querySnapshot) {
          querySnapshot.forEach(function(doc) {
              console.log(doc.id, ' =&gt; ', doc.data());
              options.push({
                  value: doc.data().title.replace(/( )/g, ''),
                  label: doc.data().title + ' - ABS ' + doc.id
              });
              });
          });
  
  
          let anzic = [];
          await fsDB.collection("anzic_codes").get().then(function (querySnapshot) {
          querySnapshot.forEach(function(doc) {
              console.log(doc.id, ' =&gt; ', doc.data());
              anzic.push({
                  value: doc.data().Title.replace(/( )/g, ''),
                  label: doc.data().Title + ' - ANZIC ' + doc.id
              });
              });
          });
  
  
              await fsDB
              .collection("newsletter")
              .get().then((querySnapshot) =&gt; {
                  querySnapshot.forEach((doc) =&gt; {
                  console.log(`${doc.id} =&gt; ${doc.data().email}`);
                  }
              );
              });
  
          this.setState({
              options,
              anzic
          });
      }
  </code></pre>
  
  <p><a href="https://i.stack.imgur.com/frwJs.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/frwJs.png" alt="enter image description here"></a><a href="https://i.stack.imgur.com/V3HU8.png" rel="nofollow noreferrer">5</a>]<a href="https://i.stack.imgur.com/V3HU8.png" rel="nofollow noreferrer">5</a></p>