-1

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.

James
  • 25
  • 2
  • 7
  • 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. – James Feb 08 '18 at 15:41
  • 1
    I stand correceted... returnUpdates(dbo,"00:00:00:00:00:00").then(function(arr){console.log(arr)}); solves it. Thank you. – James Feb 08 '18 at 15:43

1 Answers1

0

Try this

function returnUpdates(dbo, key) {
let query = {};
var myQuery;
query[key] = {$exists: true};
var myQuery = 
dbo.collection("updates").find(query).toArray().then(function(log){myQuery = 
log});
};