我正在编写用户注册表单。在node-js node mysql plugin中,我是否必须编写这样的嵌套代码?
var query1 = connection.query('SELECT * FROM table1 WHERE var1 = ?', [var1], function(err, results1) {
if(results1[0]) {
var query2 = connection.query('SELECT * FROM table2 WHERE var2 = ?', [var2], function(err, results2) {
if(results2[0]) {
var query3 = connection.query('SELECT * FROM table2 WHERE var3 = ?', [var3], function(err, results3) {
if(results3[3]) {
// do it.
}
});
}
});
}
});
编写嵌套代码真的很无聊。有没有这样的方法可以进行多次查询?
var query1 = connection.query('SELECT * FROM table1 WHERE var1 = ?', [var1], function(err, results1) { });
var query2 = connection.query('SELECT * FROM table1 WHERE var2 = ?', [var2], function(err, results2) { });
var query3 = connection.query('SELECT * FROM table1 WHERE var3 = ?', [var3], function(err, results3) { });
if(results1[0]) {
} else if(results2[0]) {
} else if(results3[0]) {
} else {
// do it.
}