如何重构此 node.js 代码以避免重复
How to refactor this node.js code, to avoid duplication
我正在尝试为我的演示项目设置一个基本框架并编写我的第一个 node.js 程序。下面的代码适用于我的第一个测试,但它有重复 - 获取连接、执行查询(但不同的查询)、解析 sql 输出。我想尽量减少这种情况,因为,我必须编写更多这样的 if-else 块,它会变成庞大而肮脏的代码。
数据库连接细节在DatabaseManager.js
中处理
app.js
第 8 行和第 40 行:
var connect = require('./DatabaseManager');
bot.dialog('profileDialog', (session) => {
session.send('You reached the profile intent. You said \'%s\'.', session.message.text);
console.log('Creating a connection');
var userMessage = session.message.text;
// Here is the FIrst block
if (userMessage.indexOf('Email') >= 0) {
session.send('Your are looking for your email');
connect(function(connection) {
console.log('Reading rows from the Table...');
request = new Request("select Email from StudentProfile where ID=1", function(err, rowCount) {
if (err) {
console.log('ERROR in QUERY');
} else {
console.log(rowCount + ' rows');
}
connection.close();
});
request.on('row', function(columns) { // Iterate through the rows using a callback
columns.forEach(function(column) {
if (column.value === null) {
console.log('NULL');
} else {
session.send(column.value);
}
});
});
connection.execSql(request);
});
session.endDialog();
return;
} //end of email id if
//Here is the second block with repeated functionality, but a different query.
connect(function(connection) {
console.log('Reading rows from the Table...');
request = new Request("select FNAME from StudentProfile where ID=1", function(err, rowCount) {
if (err) {
console.log('ERROR in QUERY');
} else {
console.log(rowCount + ' rows');
}
connection.close();
});
request.on('row', function(columns) { // Iterate through the rows using a callback
columns.forEach(function(column) {
if (column.value === null) {
console.log('NULL');
} else {
session.send(column.value);
}
});
});
connection.execSql(request);
});
} //end of dialog
).triggerAction({
matches: 'profile'
}) //end of trigger
我想通过传递必需的参数来重构此代码,使其变得简单并可由其他功能重复使用。
我厌倦了下面的方式,但是 return (column.value);
声明不起作用:
function queryDatabase(colname) {
connect(function (connection) {
request = new Request('select Email from StudentProfile where SUID=1', function (err, rowCount) {
if (err) {
console.log('ERROR in QUERY');
console.log(err);
} else {
console.log(rowCount + ' rows');
}
connection.close();
});
request.on('row', function (columns) { // Iterate through the rows using a callback
columns.forEach(function (column) {
if (column.value === null) {
console.log('NULL');
} else {
console.log(column.value);
return (column.value);
}
});
});
connection.execSql(request);
});
}
bot.dialog('profileDialog',(session) => {
session.send('You reached the profile intent. You said \'%s\'.', session.message.text);
console.log('Creating a connection');
var userMessage = session.message.text;
if( userMessage.indexOf('Email') >= 0){
session.send('Your are looking for your email');
var messg = queryDatabase('Email');
console.log(messg);
session.endDialog();
return;
} //end of email id if
else {
session.send('Looking for something else');
session.endDialog();
return;
}
} //end of dialog
).triggerAction({
matches: 'profile'
}) //end of trigger
您可以创建一个连接、运行查询然后断开连接的函数。它应该看起来像:
function execute(query, rowProcessor) {
connect(function(connection) {
console.log('Reading rows from the Table...');
request = new Request(query, function(err, rowCount) {
if (err) {
console.log('ERROR in QUERY');
} else {
console.log(rowCount + ' rows');
}
connection.close();
request.on('row', rowProcessor);
});
}
function rowProcessor(columns) {
columns.forEach(function(column) {
if (column.value === null) {
console.log('NULL');
} else {
session.send(column.value);
}
});
}
if (userMessage.indexOf('Email') >= 0) {
session.send('Your are looking for your email');
execute("select Email from StudentProfile where ID=1", rowProcessor);
// ...
I tired the below way, but return (column.value);
statement is not working
是的,您不能在此处使用 return
。既因为它是异步的,又因为它可能 运行 在 forEach
循环中多次。
改为使用回调:
var connect = require('./DatabaseManager');
function queryDatabase(query, callback) {
// ^^^^^^^^
connect(function(connection) {
console.log('Reading rows from the Table...');
const request = new Request(query, function(err, rowCount) {
// ^^^^^ use local variable
if (err) {
console.log('ERROR in QUERY');
} else {
console.log(rowCount + ' rows');
}
connection.close();
});
request.on('row', function(columns) {
columns.forEach(function(column) {
if (column.value === null) {
console.log('NULL');
} else {
callback(column.value);
// ^^^^^^^^
}
});
});
connection.execSql(request);
});
}
bot.dialog('profileDialog', (session) => {
session.send('You reached the profile intent. You said \'%s\'.', session.message.text);
console.log('Creating a connection');
var userMessage = session.message.text;
if (userMessage.indexOf('Email') >= 0) {
session.send('Your are looking for your email');
queryDatabase("select Email from StudentProfile where ID=1", function(value) {
// ^^^^^^^^^^^^^^^^^
session.send(value);
});
session.endDialog();
} else {
queryDatabase("select FNAME from StudentProfile where ID=1", function(value) {
// ^^^^^^^^^^^^^^^^^
session.send(value);
});
}
}).triggerAction({
matches: 'profile'
})
我正在尝试为我的演示项目设置一个基本框架并编写我的第一个 node.js 程序。下面的代码适用于我的第一个测试,但它有重复 - 获取连接、执行查询(但不同的查询)、解析 sql 输出。我想尽量减少这种情况,因为,我必须编写更多这样的 if-else 块,它会变成庞大而肮脏的代码。
数据库连接细节在DatabaseManager.js
中处理app.js
第 8 行和第 40 行:
var connect = require('./DatabaseManager');
bot.dialog('profileDialog', (session) => {
session.send('You reached the profile intent. You said \'%s\'.', session.message.text);
console.log('Creating a connection');
var userMessage = session.message.text;
// Here is the FIrst block
if (userMessage.indexOf('Email') >= 0) {
session.send('Your are looking for your email');
connect(function(connection) {
console.log('Reading rows from the Table...');
request = new Request("select Email from StudentProfile where ID=1", function(err, rowCount) {
if (err) {
console.log('ERROR in QUERY');
} else {
console.log(rowCount + ' rows');
}
connection.close();
});
request.on('row', function(columns) { // Iterate through the rows using a callback
columns.forEach(function(column) {
if (column.value === null) {
console.log('NULL');
} else {
session.send(column.value);
}
});
});
connection.execSql(request);
});
session.endDialog();
return;
} //end of email id if
//Here is the second block with repeated functionality, but a different query.
connect(function(connection) {
console.log('Reading rows from the Table...');
request = new Request("select FNAME from StudentProfile where ID=1", function(err, rowCount) {
if (err) {
console.log('ERROR in QUERY');
} else {
console.log(rowCount + ' rows');
}
connection.close();
});
request.on('row', function(columns) { // Iterate through the rows using a callback
columns.forEach(function(column) {
if (column.value === null) {
console.log('NULL');
} else {
session.send(column.value);
}
});
});
connection.execSql(request);
});
} //end of dialog
).triggerAction({
matches: 'profile'
}) //end of trigger
我想通过传递必需的参数来重构此代码,使其变得简单并可由其他功能重复使用。
我厌倦了下面的方式,但是 return (column.value);
声明不起作用:
function queryDatabase(colname) {
connect(function (connection) {
request = new Request('select Email from StudentProfile where SUID=1', function (err, rowCount) {
if (err) {
console.log('ERROR in QUERY');
console.log(err);
} else {
console.log(rowCount + ' rows');
}
connection.close();
});
request.on('row', function (columns) { // Iterate through the rows using a callback
columns.forEach(function (column) {
if (column.value === null) {
console.log('NULL');
} else {
console.log(column.value);
return (column.value);
}
});
});
connection.execSql(request);
});
}
bot.dialog('profileDialog',(session) => {
session.send('You reached the profile intent. You said \'%s\'.', session.message.text);
console.log('Creating a connection');
var userMessage = session.message.text;
if( userMessage.indexOf('Email') >= 0){
session.send('Your are looking for your email');
var messg = queryDatabase('Email');
console.log(messg);
session.endDialog();
return;
} //end of email id if
else {
session.send('Looking for something else');
session.endDialog();
return;
}
} //end of dialog
).triggerAction({
matches: 'profile'
}) //end of trigger
您可以创建一个连接、运行查询然后断开连接的函数。它应该看起来像:
function execute(query, rowProcessor) {
connect(function(connection) {
console.log('Reading rows from the Table...');
request = new Request(query, function(err, rowCount) {
if (err) {
console.log('ERROR in QUERY');
} else {
console.log(rowCount + ' rows');
}
connection.close();
request.on('row', rowProcessor);
});
}
function rowProcessor(columns) {
columns.forEach(function(column) {
if (column.value === null) {
console.log('NULL');
} else {
session.send(column.value);
}
});
}
if (userMessage.indexOf('Email') >= 0) {
session.send('Your are looking for your email');
execute("select Email from StudentProfile where ID=1", rowProcessor);
// ...
I tired the below way, but
return (column.value);
statement is not working
是的,您不能在此处使用 return
。既因为它是异步的,又因为它可能 运行 在 forEach
循环中多次。
改为使用回调:
var connect = require('./DatabaseManager');
function queryDatabase(query, callback) {
// ^^^^^^^^
connect(function(connection) {
console.log('Reading rows from the Table...');
const request = new Request(query, function(err, rowCount) {
// ^^^^^ use local variable
if (err) {
console.log('ERROR in QUERY');
} else {
console.log(rowCount + ' rows');
}
connection.close();
});
request.on('row', function(columns) {
columns.forEach(function(column) {
if (column.value === null) {
console.log('NULL');
} else {
callback(column.value);
// ^^^^^^^^
}
});
});
connection.execSql(request);
});
}
bot.dialog('profileDialog', (session) => {
session.send('You reached the profile intent. You said \'%s\'.', session.message.text);
console.log('Creating a connection');
var userMessage = session.message.text;
if (userMessage.indexOf('Email') >= 0) {
session.send('Your are looking for your email');
queryDatabase("select Email from StudentProfile where ID=1", function(value) {
// ^^^^^^^^^^^^^^^^^
session.send(value);
});
session.endDialog();
} else {
queryDatabase("select FNAME from StudentProfile where ID=1", function(value) {
// ^^^^^^^^^^^^^^^^^
session.send(value);
});
}
}).triggerAction({
matches: 'profile'
})