I am trying to allow a user to input criteria (i.e. Client) for my application to query a mySQL database and return all matching results. My connection is established and I am able to hardcode my query (connection.query("select * from ACHLookerUpper where Client = ?", ["ex."]) and return expected results. I can also simply run the Prompt command and intake and console.log my input.
However, when I try to combine the two -- intake user input via Prompt and query -- I cannot successfully pass input into my query. I am somewhat new to node so I'm sure there's an issue in my nesting, however the error is not very helpful other than telling me "Cannot enqeue Query after invoking Quit".
I'm sure I need to store results.Client as a var or param then call it in my query but can't figure out how.
var mysql = require("mysql");
var prompt = require('prompt');
var connection = mysql.createConnection({
host: "<host>",
user: "<user>",
password: "<pw>",
database: "<db>",
port: "<port>"
});
connection.connect(function(err){
if(err){
console.log('Error connecting to Db');
return;
}
});
prompt.start();
prompt.get(['Client'], function (err, result) {
if (err) { console.log("Error");
return;
};
connection.query("select * from ACHLookerUpper where Client = ?", [result.Client], exports.MyHandler = function(err, rows){
if (err) {
console.log(err);
return;
}
rows.forEach(function(result) {
console.log(result.ID, result.Name, result.Client, result.ACH);
})
})
});
connection.end(function(err) {
});
答案 0 :(得分:0)
prompt.get()
是异步的,因此首先调用connection.end()
然后稍后尝试在收到提示输入时执行查询,但此时连接已经结束。