Here I'm creating an array that have property 1, then I'm trying to filter the results with only the highest number and then output it when calling the function, but it returns undefined. I'm trying to find to figure out what I'm doing wrong here. Thanks
function calcDifference() {
table.find({
"entries": 1
}) // select entries with "entries":1
.toArray(function(err, res_spr) { // create an array of found elements
let pop = res_spr.pop(); // delete last element of the array
let storeArraySpr = []; // new array to store property "number"
for (let i = 0; i < res_spr.length; i ) {
storeArraySpr.push(res_spr[i].number);
}
var resultSpr = Math.max(...storeArraySpr); // select highest entry
return resultSpr.toFixed(8); // meant to output the result when calling the function
});
}
console.log(calcDifference()); // returns undefined
CodePudding user response:
You haven't returned anything from that function.
You should add return
before table.find
function calcDifference(){
return table.find({"entries":1}) // select entries with "entries":1
.toArray(function(err, res_spr) { // create an array of found elements
let pop = res_spr.pop(); // delete last element of the array
let storeArraySpr = []; // new array to store property "number"
for (let i = 0; i < res_spr.length; i ) {
storeArraySpr.push(res_spr[i].number);
}
var resultSpr = Math.max(...storeArraySpr); // select highest entry
return resultSpr.toFixed(8); // meant to output the result when calling the function
});
}
console.log(calcDifference());
If toArray
does not return values in that callback function. You can assign a value and return it.
Note that this approach will work if toArray
and find
do not return promises
function calcDifference(){
let result;
table.find({"entries":1}) // select entries with "entries":1
.toArray(function(err, res_spr) { // create an array of found elements
let pop = res_spr.pop(); // delete last element of the array
let storeArraySpr = []; // new array to store property "number"
for (let i = 0; i < res_spr.length; i ) {
storeArraySpr.push(res_spr[i].number);
}
var resultSpr = Math.max(...storeArraySpr); // select highest entry
result = resultSpr.toFixed(8); // meant to output the result when calling the function
});
return result; //return the final result from the callback function
}
console.log(calcDifference());
The 3rd approach, if find
and toArray
are actually promises
async function calcDifference(){
return await table.find({"entries":1}) // select entries with "entries":1
.toArray(function(err, res_spr) { // create an array of found elements
let pop = res_spr.pop(); // delete last element of the array
let storeArraySpr = []; // new array to store property "number"
for (let i = 0; i < res_spr.length; i ) {
storeArraySpr.push(res_spr[i].number);
}
var resultSpr = Math.max(...storeArraySpr); // select highest entry
return resultSpr
});
}
console.log(await calcDifference());
If toArray
does not return results, you can assign variable again, but the difference is now we have async/await
async function calcDifference(){
let result;
await table.find({"entries":1}) // select entries with "entries":1
.toArray(function(err, res_spr) { // create an array of found elements
let pop = res_spr.pop(); // delete last element of the array
let storeArraySpr = []; // new array to store property "number"
for (let i = 0; i < res_spr.length; i ) {
storeArraySpr.push(res_spr[i].number);
}
var resultSpr = Math.max(...storeArraySpr); // select highest entry
result = resultSpr
});
return result
}
console.log(await calcDifference());
CodePudding user response:
Your main issue is that calcDifference()
doesn't have a return value. To solve this you first need to change the way you call toArray()
. Like Barman pointed out in the comments:
When
toArray()
is called with a callback function, it doesn't return anything. See mongodb.github.io/node-mongodb-native/4.5/classes/… – Barmar
When you look at the documentation you'll find that it does return a promise if you do not pass a callback function. Firstly we change calcDifference
to an async function
. Then we await table.find({ "entries": 1 }).toArray()
instead of passing a callback. After that you can do your other stuff.
Do note that an async function always returns a promise, so to use the return value you either have to await
the value, or use a then()
construct if you are not in async context.
async function calcDifference() {
const res_spr = await table.find({ "entries": 1 }).toArray(); // select entries with "entries":1
let pop = res_spr.pop(); // delete last element of the array
let storeArraySpr = []; // new array to store property "number"
for (let i = 0; i < res_spr.length; i ) {
storeArraySpr.push(res_spr[i].number);
}
var resultSpr = Math.max(...storeArraySpr); // select highest entry
return resultSpr.toFixed(8); // meant to output the result when calling the function
}
// when in async context
console.log(await calcDifference());
// when not in async context
calcDifference().then(console.log);
// or if you need to execute multiple statements
calcDifference().then((result) => {
console.log(result);
// other stuff
});