NodeJS范围问题

时间:2015-03-22 13:35:12

标签: node.js

var std;
......
...... // here I fill std with some data
......

我试图访问' std'处理此路线时的数组:

app.post('/saveABS',function(req,res){
var fileName = "./public/"+f;
fs.writeFile(fileName, JSON.stringify(std, null, 4), function(err) {
    if(err) {
        console.log(err);
    } else {
        console.log("The file was saved !");
        console.log(a);
        for(var i = 0; i < std.length; i++)
        {
            var connection = mysql.createConnection({
                host     : 'localhost',
                user     : 'root',
                password : 'admin' ,
                database : 'pfe'
            });
            console.log('before hr_cours = ' + std[i].hr_cours); // <========= WORKS

这里正确显示了std [i] .hr_cours的值。

            if(std[i].hr_cours != a[i].hr_cours)
            {

                connection.connect(function(error){
                    console.log('after hr_cours = ' + std[i].hr_cours); // <============ undefined

这里显示未定义。

                    if(error)
                    {
                        console.log('Unable to connect to database.');
                    }
                    else
                    {
                        console.log("i= " + i);
                        console.log("I'm in : hr_cours = " + std[i].hr_cours);
                        connection.query("INSERT INTO assiste VALUES (CURDATE(),(SELECT id_etud FROM Etudiant WHERE nom_etud = ?),(SELECT s.id_seanceFROM Seance s, aura_lieu a, jour j, fait_objet fWHERE s.id_seance = a.id_seanceAND CURTIME() BETWEEN a.heure_debut AND a.heure_fin AND a.id_jour = j.id_jour AND j.dat = CURDATE() AND f.id_seance = s.id_seance AND f.id_ens = ?), ?)", [std[i].nom_etud, idEns, std[i].hr_cours], function(er1, res1){
                            if(er1)
                            {
                                console.log('Query error: ');
                            }
                            else
                            {
                                console.log('success');
                            }
                        });
                    }
                });
            }
        }
        res.end("all");
    }
});
});

我注意到一旦执行流程进入connection.connect块就会触发问题并且它是一个范围问题;我到处搜索答案,但无法找到解决我具体问题的方法。 我怎样才能做到这一点?

2 个答案:

答案 0 :(得分:1)

connection.connect的执行被延迟,因此触发时ii传递给for时的值不同环。因此,要在该特定循环执行中锁定i的值,请将connection.connect方法和回调包装在IIFE中并传递i作为其参数:

~function(i) {
    connection.connect(function(error){
        console.log('after hr_cours = ' + std[i].hr_cours);
        // other stuff
    })
}(i)

我在这里的答案中更详细地解释了IIFE和范围的结束:Odd javascript code execution order

探索这项技术的JSFiddle游乐场:IIFE Scope Closure With Pre-Existing Parameters

答案 1 :(得分:1)

这是你的问题:

app.post('/saveABS', function(req, res) {
    // ...
    for (var i = 0; i < std.length; i++) {                
        // ... 
        // This is SYNC. Here i = 0,1,2... 
        connection.connect(function(error) {
            // This is A-SYNC. 
            // Here i = std.length + 1  
            // always!
            // No matter even if it syntactically looks like i should be 1,2,3 according to the loop
            // but all of this is executed way after the loop has long been completed

            // ...
            // Here it displays undefined.
            console.log('after hr_cours = ' + std[i].hr_cours); // <============ undefined

解决方案:在循环中自我执行匿名函数(function(){})()并将参数传递给它(i),以便它们保持不变

for (var i = 0; i < std.length; i++) (function(i){
    connection.connect(function(error) {
        // ...
        // Here, since i is an argument passed to outer function
        // i will be i,2,3... 
})(i);

另一个解决方案:forEach循环,它已经有一个回调函数

std.forEach(function(currentValue, i, std){
    connection.connect(function(error) {
        // ...
        // Same here, since i is an argument passed to outer function
        // i will be i,2,3...     
        // Also currentValue = std[i] so you can use that 
});