title: Cannot read properties of undefined (reading 'fitness')
body: <p>We are currently making a website for our roommate matching project. We are at the point where we are making the results page, which should display the 3 most compatible roommates based on some questions where the algorithm calculates their compatibility percentage.</p>
  <p>We are now trying to pass the object called &quot;fitness&quot; that holds all of the results, which would be:</p>
  <pre><code>let fit = {
      fitness: Math.round(sum * 100),
      email: resident[i].email,
      name: resident[i].firstName + &quot; &quot; + resident[i].lastName,
  };
  </code></pre>
  <p>To our .ejs file called <em>results.ejs</em>. The fit object gets pushed into our global &quot;fitness&quot; variable.</p>
  <p>This is our post request:</p>
  <pre><code>router.post(&quot;/results&quot;, function (request, response) {
      let item = request.body;
      let applicant_name = request.body.firstName;
  
      client.connect(function (err) {
          const db = client.db(&quot;myFirstDatabase&quot;);
          assert.equal(null, err);
          db.collection(&quot;residents&quot;).insertOne(item, function (err, result) {
              assert.equal(null, err);
              console.log(&quot;Item inserted&quot;);
          });
      });
  
      client.connect(function (err) {
          const db = client.db(&quot;myFirstDatabase&quot;);
          assert.equal(null, err);
          db.collection(&quot;residents&quot;)
              .find({})
              .toArray(function (err, resident) {
                  if (err) throw err;
  
                  /* weighting(request); */
                  compatability(request, resident);
              });
      });
  
      client.close();
      response.render(&quot;pages/results&quot;, {
          applicant_name: applicant_name,
          resident: fitness,
      });
  });
  </code></pre>
  <p>In our results.ejs file, we use Bootstrap cards in order to display the 3 most compatible roommates. This is the code we use in the results.ejs file:</p>
  <pre><code>&lt;p class=&quot;card-text&quot;&gt;
      Compatibility %: &lt;%= resident[0].fitness %&gt;&lt;br /&gt;
      Name: &lt;%= resident[0].name %&gt; &lt;br /&gt;
      Email: &lt;%= resident[0].email %&gt;&lt;br /&gt;
      Location: &lt;br /&gt;
  &lt;/p&gt;
  </code></pre>
  <p>Whenever we run the post request, we get an error saying &quot;Cannot read properties of undefined (reading 'fitness')&quot;, but if we refresh again, the Node.js server crashes, but it displays the actual values we want to get. If we add a &quot;?&quot; after &quot;resident[0]&quot;, it runs the page without any errors but does not display any information from the object.
  We have tried numerous things but cannot find the right solution to solve this issue.
  Thanks in advance.</p>
  <p><strong>EDIT</strong></p>
  <p>The whole code from the .ejs file and our serverside code, with photos of how it looks on the website: <a href="https://pastebin.com/zRxdk6aq" rel="nofollow noreferrer">https://pastebin.com/zRxdk6aq</a></p>