I am a beginner to node.js and I have been working on a simple cube timer but unfortunately, something doesn't work. In app.get when I run sumOfSeconds(best) an error occurs telling me that fun (last line in sumOfSolves) is not a function. However when I tried running sumOfSeconds outside of app.get (i also change the return to console.log so I can see the result) everything works. What is the problem? Thanks in advance ;)
var sumOfSolves = [];
//create an array of total seconds in each time - used for Ao5, Ao12, best and worst
function sumOfSeconds(fun){
Time.find({}, function(err, data){
if(err) throw err;
sumOfSolves = [];
for(var i = 0; i < data.length; i++){
sum = data[i].min * 60 + data[i].sec;
sumOfSolves.push(sum);
}
fun(); //this makes best(), worst(), Ao5(), Ao12 execute after sumOfSeconds finished everything else,
//otherwise those functions run before the sumOfSolves array had something in ot and therefore the code didn't work
})
}
function best(){
var best = Math.min(...sumOfSolves);
var position = sumOfSolves.indexOf(best);
Time.find({}, function(err, data){
if(err) throw err;
var bestTime = data[position].time;
return bestTime
})
}
function worst(){
var worst = Math.max(...sumOfSolves);
var position = sumOfSolves.indexOf(worst)
Time.find({}, function(err, data){
if(err) throw err;
var worstTime = data[position].time;
return worstTime;
})
}
function Ao5(){
}
function Ao12(){
}
module.exports = function(app){
app.get('/', function(req, res){
var best = sumOfSeconds(best);
var worst = sumOfSeconds(worst);
Time.find({}, function(err, data){
if(err) throw err;
res.render('index', {times: data, best: best, worst: worst});
})
});