javascript无法设置变量值

时间:2015-10-14 18:51:06

标签: javascript

这个问题可能听起来很愚蠢,但我试了一个小时左右但却无法实现。我试图将值设置为全局变量并且即时失败。我知道它的范围概念。我需要确定哪里出错了...任何帮助都非常感谢。感谢。

router.get("/details/:user_id", function(req, res) {


var userId = req.params.user_id;
var sql1 = "SELECT * FROM MOBILE_USERS WHERE ID = ?";
var sql2 = "SELECT * FROM USER_PROFILE WHERE UID  = ?";
var globalArray= null;
//FUNCTION 1
db.getData(sql1,[userId], function(err, results) { //CALLBACK FN
    if(err) { res.status(500).send("Server Error"); return;}
    // Respond with results as JSON
    if(results.length >0) {
      globalArray = results; //set for json array

    }else{
        res.json(0);
    }

});
 FUNCTION 2
db.getData(sql2,[userId],function(err, results) { // Call back function
    if(err) { res.status(500).send("Server Error"); return;}
    // Respond with results as JSON 
    if(results.length >0) {
        globalArray.push(results[0]); //push new values from second function
    }else{
        res.json(0);
    }

});
res.json(globalArray); //send JSON array -> THIS SHOULD CONTAIN VALUES FROM BOTH FUNCTION 1 and 2

});

0 个答案:

没有答案