title: In Next.js, I can't get the mongoDB document on the same line of my query?
body: <p>I tried the following on my Next.js backend, and I have MongoDB collection named &quot;users&quot;. I want to find the user that has the maximum id.</p>
  <pre><code>const max_id = await users.find({}).sort({&quot;id&quot;:-1}).limit(1).toArray()[0][&quot;id&quot;]
  </code></pre>
  <p>I get the error of <code>unhandledRejection: TypeError: Cannot read properties of undefined (reading 'id')</code> which indicates this entire statement returns none.</p>
  <p>However, if I do the following in two lines, everything works as intended:</p>
  <pre><code>const newest_user = await users.find({}).sort({&quot;id&quot;:-1}).limit(1).toArray();
  const max_id = newest_user[0][&quot;id&quot;];
  </code></pre>
  <p>in my first example that doesn't work, is that because <strong>users.find({}).sort({&quot;id&quot;:-1}).limit(1).toArray()</strong> is a promise, it will take time to resolve. But before the promise resolves, the promise's value is undefined, and <strong>undefined[0][&quot;id&quot;]</strong> will throw an error.</p>
  <p>Please correct me.</p>