无法在Node.js中的mysql查询中访问变量

时间:2018-10-09 06:25:35

标签: node.js botframework mysql2

嗨,我正在将bot框架的会话变量传递给此函数,并且该函数工作正常,但是当我尝试在conn.query(内部函数)中访问它时,它是不可访问的。这是代码段

function hopitalSearch(city, treatment, context, callback) {
    conn = mysql.createConnection(db_config);
    conn.connect(
    function (err) { 
        if (err) { 
            console.log("!!! Cannot connect !!! Error:");
            throw err;
        } 
    });
    var hospitalList = [];
    var basic_query = 'SELECT hospital_name FROM hospital_list WHERE hospital_location="' + city + '" AND treatment_name="' + treatment + '" LIMIT 10';
    conn.query(basic_query, function (err, results, fields) {
        if (err) throw err;
        for (var i = 0; i < results.length; i++) {
            hospitalList.push(JSON.stringify(results[i].hospital_name));
        }
        console.log(hospitalList);
        console.log("showing context")
        console.log(context); //not accessible
    }); }

0 个答案:

没有答案