Getting Users From Mongo Atlas

Hello, I was hoping someone can help me with this. I am trying to get an array of users to hand off to Passport from my Atlas Cluster. I basically copied this code straight from the Atlas Docs (except the .env variables).

I run into 2 problems:

  1. I cant get the last console .log to wait for the run function to finish. I get a pending promise, and I can’t “await” the run().
  2. The commented out Timeout code will give me the users but then its still a Promise. And nothing I have done with a .then statement has managed to get anything but another Promise back. I feel like I am missing something fundamental here.

What I am trying to get:
const users = [
{
id: ‘1661622049062’,
name: “John Doe”,
email: “test@test.com”,
password: “PW1”
},
{
id: ‘1661622093607’,
name: “Jane Doe”,
email: “test2@test2.com”,
password: “PW2”
}
]

Code:

Please post your actual code instead of a picture. Thanks

My first question is why re you using setTimeout?

Hi Jeremy, sorry about that. Here is the actual code.

const { MongoClient } = require("mongodb")
require("dotenv").config()

const uri = process.env.MONGO_URI
const collection = process.env.MONGO_DB
const cluster = process.env.MONGO_CLUSTER

const client = new MongoClient(uri)

async function run() {
  try {
    const database = client.db(collection)
    const coll = database.collection(cluster)
    const cursor = coll.find()
    
    var results = await cursor.toArray()
  } finally {
    await client.close()
  }
//   console.log(typeof(results))
  return results
}

const users = run().catch(console.dir)

setTimeout(() => { console.log(users) }, 5000)
// console.log(users)

I’ve edited your post for readability. When you enter a code block into a forum post, please precede it with a separate line of three backticks and follow it with a separate line of three backticks to make it easier to read.

You can also use the “preformatted text” tool in the editor (</>) to add backticks around text.

See this post to find the backtick on your keyboard.
Note: Backticks (`) are not single quotes (').

Hi RandellDawson,
The Timeout gives me a fulfilled Promise instead of a pending one. If I just console.log(user) I get “Promise {}”

You might benefit from reading this thread.

Thank you. I will read it now.