简体   繁体   中英

Node.js callback function doesn't work

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});
        })
    });

You declare best and worst as var in app.get . Don't do this! Choose unique names for your variables that don't conflict with the function declarations.

Edit: Scope in Javascript is as important as in any other language, maybe more so because the entire script is essentially global scope (the global scope has access to variables declared in a nested scope).

Also, almost everything is a JS Object. (See https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures for the 6 primitive types.) So, unlike in languages that allow both a variable and a function to have the same name, in JS, there can be only one identifier of a given name. You "hoisted" the names best and worst , so the JS engine used your new definition instead of the functions.

If you want to avoid these kinds of issues in the future, and don't have to worry about supporting older browsers, use let instead of var . let warns you about hoisting.

Also, at least during development, put: "use strict"; at the top of your source file.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM