【发布时间】:2014-10-03 12:04:52
【问题描述】:
我正在编写一段代码来从 DB 构建模式,并基于 SE 上的其他示例,我想出了这种方法将表名一直传递到我将处理表字段的代码(我当时需要访问表名)。架构来自 MySQL,使用 jugglingdb-mysql。
这是我能做的最“优雅”的事情吗(我不希望代码中像现在这样写闭包)?
schema.client.query('SHOW TABLES', function(err, data) {
if (err) throw err;
data.forEach(function(table) {
// closure to pass down table name
(function(_table) {
schema.client.query('SHOW columns FROM ' + _table, function(err, rows) {
if (err) throw err;
// closure to pass down table name
(function(_table) {
rows.forEach(function(row){
// Go wild processing fields
})
})(_table);
});
})(table['Tables_in_db'])
});
});
【问题讨论】:
标签: javascript mysql node.js closures