查询数据库时,出现此错误:
TypeError: Cannot call method 'query' of undefined
at Object.<anonymous> (/home/nodeuser/myapp_node/index.js:51:17)
at Module._compile (module.js:456:26)
at Object.Module._extensions..js (module.js:474:10)
at Module.load (module.js:356:32)
at Function.Module._load (module.js:312:12)
at Function.Module.runMain (module.js:497:10)
at startup (node.js:119:16)
at node.js:901:3
我的代码如下所示:
var mysql = require('mysql');
var mysqlConnection = mysql.createConnection({
host : 'localhost',
user : 'root',
password : '',
database : 'carousel',
debug : true,
}).connect(function(err) {
if ( !err ) {
console.log("Connected to MySQL");
} else if ( err ) {
console.log(err);
}
});
var userId = 23
mysqlConnection.query('SELECT * FROM tusers WHERE id = ?', [userId], function(err, results) {
console.log(err);
console.log(results);
});
我确保“mysql”在 node_modules 中。此外,我在控制台中看到“已连接到 MySQL”。
你知道我该怎么做吗?