我有一些场景需要先保存用户,一旦用户保存,我需要将用户id(用户表的主键)绑定为users_details表的forign键。
所以我的逻辑是有两个创建操作。一个用于存储用户,另一个用于存储用户详细信息但这里的第二个创建操作取决于第一个创建操作的结果。
但问题是每当我执行第一次创建等待结果并且并行执行某些代码时。
我有单独的文件来分离逻辑
获取连接
var mysql = require("mysql");
var connection = {};
connection.getConnection = function(){
var con = mysql.createConnection({
host: "localhost",
user: "root",
password: "******",
database: "*****"
});
con.connect(function(err){
console.log("10");
if(err){
console.log('Error connecting to Db');
return;
}
console.log('Connection established');
});
return con;
}
module.exports = connection;
用于一般CR操作
var connection = require("./connection.js");
var execute = {};
execute.insertQuery = function(query, values){
var result;
// getting connection
var con = connection.getConnection();
// General insert query
con.query(query, values, function(err,rows){
if(err) throw err;
console.log('Data inserted into table:\n');
console.log(JSON.stringify(rows));
// Send the response body
result = rows;
console.log("Result========= " + result);
return result;
});
// conncetion close
con.end(function(err) {
// The connection is terminated gracefully
// Ensures all previously enqueued queries are still
// before sending a COM_QUIT packet to the MySQL server.
});
console.log("Result========= " + result);
return result;
}
module.exports = execute;
这是语句创建
router.post('/registerUser', function(req,res){
saveUser();
function saveUser(){
var selectQuery = 'select username from users where username = ?';
var values = [username];
var usernameUsed = execute.selectQuery(selectQuery, values);
if(usernameUsed){
req.flash('error_msg', 'Username is not availble, Please use different username.');
res.redirect('/users/register');
}else{
var users = 'INSERT INTO users (username, password) VALUES (?, ?)';
var values = [username, password];
var inserted = execute.insertQuery(users, values);
if(inserted){
var users_details = 'INSERT INTO users_details (firstname, lastname, email, users_id) VALUES (?, ?, ?, ?)';
var data = [firstname, lastname, email, inserted.insertId];
var result = execute.insertQuery(user_details, data);
if(result){
req.flash('success_msg', 'Congratulations. You are registered sucessfully.');
res.setStatus = 200;
res.redirect('/users/login');
}else{
req.flash('error_msg', 'Sorry something went wrong, We are looking into it.');
res.redirect('/users/register');
}
}else{
req.flash('error_msg', 'Sorry something went wrong, We are looking into it 2.');
res.redirect('/users/register');
}
}
}
});
答案 0 :(得分:0)
您可以使用async module
的waterfall
方法
async.waterfall([
function (next) {
execute.insertQuery(users, values,next);
},function(user_details,next){
execute.insertQuery(user_details, data,next);
}
], function(err,user_details){
if(!err){
//do your stuff with result
execute.insertQuery(user_details, data);
//send response from here or you can call function here and send response from function defination
}
})
execute.insertQuery function(user,values,next){
//if you get any error pass to callback
next(err)
//Do your stuff and after successfull result send result in next callback
next(null,user_details);
}
execute.insertQuery function(user_dtails,data,next){
//if you get any error pass to callback
next(err)
//Do your stuff and after successfull result send result in next callback
next(null,user_details);
}
注意: 如果您在第二个或第三个功能中需要data
值,您可以自行联系,以便您无法获得错误
希望这对你有用