I'm having an issue with getting data out of a MongoDB collection and return it into an array in my code in NodeJS.
The code so far returns a promise, that I don't fully know how to handle.
Any help would be appreciated.
async function loadData() {
try {
// Connect to the MongoDB cluster
console.log('Attempting to connect to DB')
client.connect(async err => {
const collection = client.db("db").collection("collection");
await collection.find({}).toArray().then((data) => {
//Getting stumped as the data gets returned as a promise and does not get added to an array
}
);
client.close();
console.log('Closed DB connection');
});
} catch (e) {
console.error(e);
}