title: Why does an array that I am assigning to the return value of a Javascript fetch contain data that isn't even showing up in the response?
body: <p>I apologize if a similar issue was asked elsewhere, as I didn't even really know how to ask this question or what to look for since I haven't really come across anything like this before.</p>
  <p>So I will try to make this simple, I am making a fetch call (GET) from my frontend ReactJS webapp to my Java Spring API that sits on top of a MySQL database. I am setting an array to the return value of the fetch call since the return value is an array of objects (a list of &quot;groups&quot; if you care). I am using async await on the function which is being called within the componentDidMount lifecycle event.</p>
  <p>Essentially what is happening that I cannot figure out is that, although in the Network tab I can clearly see that only one &quot;group&quot; object is being returned, in the array it shows two when I console log it. Whats even weirder is that in the second .then() callback when I console log JSON.parse(text) it shows what I would expect to be returned - a single &quot;group&quot; object. But then when I assign my groups variable to JSON.parse(text), then it shows two &quot;group&quot; objects instead of just the one I am expecting. One thing I have done is moved the fetch call out of the getGroups() function and placed it directly in componentDidMount() and then it worked fine. This leads me to believe that I am screwing something up with how async await should be setup. I also change var groups to const groups (obviously had to directly assign const groups to the response, so it was limited to the scope of the .then() callback) and that also gave me the expected result.</p>
  <p>I have another function called searchGroups() where I am having the same exact problem with the same exact endpoint. I'll post some code below. If theres anything else you need to see please just let me know.</p>
  <p>Would really appreciate some help here, been working at figuring this out for a couple days now... otherwise I might have to go Spider Man III Tobey Maguire dark spidey on this thing.</p>
  <pre><code>async componentDidMount(){ 
      await this.getGroups();
  }
  
  //&lt;Some other lifecycle events here...&gt;
  
  async getGroups(){
      var ownerID = localStorage.getItem(&quot;userID&quot;);
      var groups = [];
      var sharedGroups = [];
      
      await fetch(baseURI + &quot;/api/group/forOwner/&quot; + ownerID, {  
          method: &quot;GET&quot;,                          
          headers: {&quot;Content-Type&quot;: &quot;application/json&quot;,
                    &quot;Authorization&quot;: &quot;Bearer &quot; + localStorage.getItem(&quot;auth_token&quot;)}
      })
      .then(res =&gt; res.text())
      .then(
          (text) =&gt; {
              var result = text.length ? JSON.parse(text) : {};
              groups = result;
          }
      ).catch(console.log);
  
      await fetch(baseURI + &quot;/api/group/forTeacher/&quot; + ownerID, {  
          method: &quot;GET&quot;,                          
          headers: {&quot;Content-Type&quot;: &quot;application/json&quot;,
                    &quot;Authorization&quot;: &quot;Bearer &quot; + localStorage.getItem(&quot;auth_token&quot;)}
      })
      .then(res =&gt; res.text())
      .then(
          (text) =&gt; {
              var result = text.length ? JSON.parse(text) : {};
              sharedGroups = result;
              console.log(sharedGroups);
          }
      ).catch(console.log);
  
      if(groups.length === undefined){
          groups = [];
      }
  
      if(sharedGroups.length === undefined){
          sharedGroups = [];
      }
  
      this.setState({
          ownerGroups: groups,
          sharedGroups: sharedGroups
      });
  }
  
   //&lt;Some more functions here...&gt;
  
   async searchGroups(){
  
      document.getElementsByClassName(&quot;loaderBackground&quot;)[0].style.display = &quot;flex&quot;;
  
      var list = document.getElementById(&quot;groupList-Admin&quot;);
      var count = 0;
      var searchText = document.getElementsByClassName(&quot;Fit4Life-Searchbar-Admin&quot;)[0].value.trim();
      var ownerID = localStorage.getItem(&quot;userID&quot;);
  
      var groups = [];
      var sharedGroups = [];
  
      while (list.firstChild) {
          list.removeChild(list.lastChild);
      }
  
      await fetch(baseURI + &quot;/api/group/forOwner/&quot; + ownerID, {  
              method: &quot;GET&quot;,                          
              headers: {&quot;Content-Type&quot;: &quot;application/json&quot;,
                        &quot;Authorization&quot;: &quot;Bearer &quot; + localStorage.getItem(&quot;auth_token&quot;)}
          })
          .then(res =&gt; res.text())
          .then(
              (text) =&gt; {
                  groups = text.length ? JSON.parse(text) : {};
              }
          ).catch(console.log);
  
          await fetch(baseURI + &quot;/api/group/forTeacher/&quot; + ownerID, {  
              method: &quot;GET&quot;,                          
              headers: {&quot;Content-Type&quot;: &quot;application/json&quot;,
                        &quot;Authorization&quot;: &quot;Bearer &quot; + localStorage.getItem(&quot;auth_token&quot;)}
          })
          .then(res =&gt; res.text())
          .then(
              (text) =&gt; {
                  var result = text.length ? JSON.parse(text) : {};
                  sharedGroups = result;
              }
          ).catch(console.log);
  
       //&lt;Rest of searchGroups() function and class code...&gt;
  </code></pre>
  <p>response according to console.log:
  <a href="https://i.stack.imgur.com/r5Iys.png" rel="nofollow noreferrer">console.log response</a></p>
  <p>response according to network tab:
  <a href="https://i.stack.imgur.com/eSD8x.png" rel="nofollow noreferrer">network tab response</a></p>