i hope someone can help a JavaScript-Newbie. I am trying to retrieve data from a MongoDB and return the result in a function.
test()
{
var list = [];
MongoClient.connect(url, (err, client) => {
if (err) throw err;
const db = client.db("test");
db.listCollections().toArray().then((docs) => {
list = docs;
}).catch((err) => {
console.log(err);
}).finally(() => {
client.close();
});
});
return list;
}
When i call console.log(test()) the list is always empty. I know, thats because its an asynchronous function. But how can i solve the Problem?