I'm not sure what I'm missing here. I would like to query a MongoDB Database within a Nodejs function. The jobs
variable below, keeps returning undefined. I'm expecting it to return an array. If I run a console.log within collection.find
it outputs the array I'm trying to return.
async function getDataFromMongoDB(page) {
const MongoClient = require("mongodb").MongoClient;
const uri = "mongodb://localhost:3001";
const client = new MongoClient(uri, { useNewUrlParser: true });
client.connect(async function(err) {
console.log(5, err);
const collection = client.db("meteor").collection("jobs");
const jobs = await collection.find().toArray((err, items) => {
return items;
});
console.log("jobs", jobs);
// return jobs;
// console.log(jobs);
// perform actions on the collection object
client.close();
});
}