我试图从Nodejs中的mysql数据库中获取结果查询,但是'results.affectedRows'始终是'undefined'。我目前正在使用Mac。希望您有兴趣回答我的问题。遵循我的代码:
const expres = require('express');
const mysql = require('mysql');
const app = expres();
var connection = mysql.createConnection({
host: "localhost",
user: "root",
password: "",
database: "sistemizin"
});
console.log("Connected!");
app.set('view engine', 'hbs'); app.use(expres.static('publik'));
app.get('/', (req, res) => {
res.render('cobaview');
});
app.post('/', (req, res) => {
var contoh = `SELECT namamatkul FROM matkul_dosen WHERE idmatkuldosen = 70`;
connection.query(contoh, (err, results, fields) => {
if (err) {
return console.error(err.message);
}
console.log('Results counted:' + results.affectedRows);
});
connection.end();
res.end();
});
app.listen(8084);
console.log("server is running at http://127.0.0.1:8084/");
答案 0 :(得分:0)
results.affectedRows
仅在插入,更新或删除查询的情况下返回。由于您是选择查询,因此结果符合预期。