我正在学习在node.js中使用mysql
模块,因此我将其用于Express
& Mustache
呈现一个MySQL表并提出了这个:
var express = require('express');
var app = express();
var mu2 = require('mu2');
mu2.root = __dirname + '/views';
var mysql = require('mysql');
var con = mysql.createConnection({
host: "localhost",
user: "root",
password: "root",
port: 3306,
database: 'breakthrough'
});
con.connect(function (err) {
if (err) {
console.log('Error connecting to database:\n' + err);
return;
}
});
app.get('*', function (req, res) {
var tableHTML;
function renderTable(rows) {
tableHTML = "<table>";
for (var row in rows) {
tableHTML += "<tr>";
for (var cell in row) {
tableHTML += ("<td>" + cell + "</td>");
}
tableHTML += "</tr>";
}
tableHTML += '</table>';
}
con.query('SELECT * FROM drivers', function (err, rows){
if (err) { throw(err); }
renderTable(rows);
htmlStream = mu2.compileAndRender('frontPage.html', {table: tableHTML});
htmlStream.pipe(res);
});
});
app.listen(8080, function () {
console.log("Listening on port 8080.");
});
但结果表只显示零,每行一个:
<table>
<tr><td>0</td></tr>
<!-- tr repeated for each row in rows -->
</table>
一些读数表明,迭代node.js中的对象目前存在很大问题。有什么方法可以对node.js对象进行延迟迭代吗?