for(... in ...)循环在node.js中不起作用

时间:2016-12-05 08:15:32

标签: mysql node.js iteration

我正在学习在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对象进行延迟迭代吗?

0 个答案:

没有答案