I can not figure out how to deliver this MongoDB array properly:
The following function:
console.log(returnUpdates(dbo,"00:00:00:00:00:00"));
returns an 'undefined' variable... I've tried:
function returnUpdates(dbo, key) {
let query = {};
query[key] = {$exists: true};
thisQuery = dbo.collection("updates").find(query).toArray(function(err, result) {
if (err) throw err;
console.log(result);
return result;
});
return thisQuery;
};
and
function returnUpdates(dbo, key) {
let query = {};
query[key] = {$exists: true};
dbo.collection("updates").find(query).toArray().then(function(log){console.log(log);return log});
};
which both console log the array properly from inside of the returnUpdates function, but logs an undefined variable outside of the returnUpdates function. I've also tried:
function returnUpdates(dbo, key) {
let query = {};
query[key] = {$exists: true};
var myQuery = dbo.collection("updates").find(query).toArray().then(function(log){console.log(log);return log});
return myQuery;
};
which returns a pending promise.
From what I can tell, the array has arrived at the time that I make a call for it... seeing as console.log() returns the array.
I'm unclear as to what I'm doing incorrectly. I just want to return the array so that I can pass it to additional functions.