node.js - Execution of code in order for some purpose in node js -


i have scenario need first save user, once user saved need bind users id (primary key of user table ) forign key users_details table.

so logic having 2 create operations. 1 store user , second store user details. here second create operation depend on result of first create operation.

but issue whenever executing first create waiting result , parallel executing code.

i have separate file separation of logic

  1. to connection

    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 db');         return;     }     console.log('connection established'); });  return con; }  module.exports = connection; 
  2. for general cr operations

    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 table:\n');    console.log(json.stringify(rows));     // send response body    result = rows;    console.log("result========= " + result);    return result;   });  // conncetion close con.end(function(err) {   // connection terminated gracefully   // ensures enqueued queries still   // before sending com_quit packet mysql server. });  console.log("result========= " + result); return result; }     module.exports = execute; 
  3. this statement creation

    router.post('/registeruser', function(req,res){  saveuser();  function saveuser(){      var selectquery = 'select username users username = ?';     var values = [username];      var usernameused = execute.selectquery(selectquery, values);                      if(usernameused){         req.flash('error_msg', 'username not availble, please use different username.');         res.redirect('/users/register');     }else{          var users = 'insert users (username, password) values (?, ?)';         var values = [username, password];          var inserted = execute.insertquery(users, values);          if(inserted){              var users_details = 'insert 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. registered sucessfully.');                 res.setstatus = 200;                 res.redirect('/users/login');             }else{                 req.flash('error_msg', 'sorry went wrong, looking it.');                 res.redirect('/users/register');             }         }else{             req.flash('error_msg', 'sorry went wrong, looking 2.');             res.redirect('/users/register');         }     } } }); 

you can use waterfall method of async module

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 stuff result       execute.insertquery(user_details, data);       //send response here or can call function here , send response function defination       }    })      execute.insertquery function(user,values,next){    //if error pass callback     next(err)     //do stuff , after successfull result send result in next callback    next(null,user_details);    }      execute.insertquery function(user_dtails,data,next){    //if error pass callback     next(err)     //do stuff , after successfull result send result in next callback    next(null,user_details);    } 

note: if need data value in second or third function can make reachable don't error

hope work you


Comments

Popular posts from this blog

java - Jasper subreport showing only one entry from the JSON data source when embedded in the Title band -

serialization - Convert Any type in scala to Array[Byte] and back -

SonarQube Plugin for Jenkins does not find SonarQube Scanner executable -