JS回调范围

时间:2016-01-22 12:37:20

标签: javascript scope

我正在Node.js和Express中实现一个小型的Web服务器,我想知道如何在数据库回调中访问数据变量? 整个代码在快速路由函数内,db是sqlite3

var data = {
    color: "blue",
}

db.all("SELECT * FROM connections WHERE ip='" + req.connection.remoteAddress + "' AND connection='connected'",function(err,rows){
    console.log(rows.length)
    if (rows.length > 0) {
        data["color"] = "green";
    }else {
        data["color"] = "red";
    }
});

0 个答案:

没有答案