I have the following code using a MongoClient getting called twice:
async function readWhileRunning(dbName, collectionName, query, projection) {
const collection = await getCollection(dbName, collectionName);
console.log("Here3")
return await collection.find(query, projection).toArray();
}
async function getCollection(dbName, collectionName) {
console.log("Here1")
const db = client.db(dbName);
console.log("Here2")
return db.collection(collectionName);
}
For some reason it produces the following output (alongside an empty array of database records):
Here1
Here2
Here1
Here2
Here3
Here3
Looks like db.collection(collectionName) refuses to run before everything else is done. I specified every function as async and every call with await. What am I doing wrong?
1