You have 2 problems.
1. No way to reassign variable passed as parameter inside a function:
var a = 1;
function fn(b) {
b = 2;
}
fn(a);
a === 1; // true
Also it's a bad pattern to mutate data inside functions when it's not really needed. Better just return and assign in outer scope:
function all() {
return card.cards.find({});
}
all(a).then(console.log); // [...]
But you can mutate an array without reassigning:
var a = [1, 2, 3];
function fn(array) {
array.splice(0, Infinity, ...[5, 6, 7, 8]);
}
a; // [1, 2, 3]
fn(a);
a; // [5, 6, 7, 8]
2. Async calls to Mongoose and async-await/promises
Still even if you return that from function all
you have to wait the result.
Calls to mongoose methods are async. It means they return Promise objects (or promises) that implements interface with methods then
and catch
. The function you pass to then
method will be called if promise will be resolved successfully, and the function in catch
will be called if the promise will be rejected.
E.g.
function p() {
if (Math.random() > 0.5) {
return Promise.resolve(1);
} else {
return Promise.reject(new Error('2'));
}
}
p()
.then(function (result) { /* will be called half of time for resolve branch */ })
.catch(function (error) { /* will be called for reject branch */ })
This code is similar to but uses syntactic sugar:
async function p() {
if (Math.random() > 0.5) return 1; // Similar to return Promise.resolve(1);
throw new Error('2'); // Similar to return Promise.reject(new Error('2'))
}
To wait promises inside async function there is an await
keyword.
async function all() {
try {
return await cards.find({});
} catch(error) {
if (false /* somehow handle mongoose errors */) {
// check error, if db not ready just retry or do sth else
// …
}
// Just rethrow if we can't do any
throw error;
}
}
Read more about async-await: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/async_function