2

I know this questions is big. but the below code is working successful by updating 3 table .but i am struck in handling, multiple callbacks. And also need help for best practice way to handle callbacks in this type of scenario.

target of work: Need to maintain Registration table, Login Table, user_info Table having common username, email info

  • After successful Registration form submit from client to Req to Node JS
  • First condition Name should not exists already in --> checked in code
  • Second condition Email should not exists already --> checked in code
  • Registration Table , Login Table , User_info Table have to fill. --> working

But main Struck point is need to update and simultaneously check for any mysql || any errors in that operation of 'INSERT INTO'.

This is route to initiate process // no Syntax errors in code - need functional point and logical point of help

    router.post('/api/register', function(req, res, next) {
    console.log(req);
    let registerData = req.body;
    let gotCallBack = false;
    dbConnection(function(err, connection) {
        if(err) {
          console.log(err);
          res.send({'error': 'Something went wrong with Connection'})
        }
        else {
          registerSave(registerData, connection, function(error, response) {
            if(!error && response == 'success') {
              res.send({'success': registerData.username});
            }
            else if(!error && (response == registerData.username || response == registerData.email)) {
              res.send({'exists': response});
            }
            else {
              if(gotCallBack) {
                return ;
              }
              else {
                gotCallBack = true;
                res.send({'error':'Something went wrong'});
              }
            }
          });
        }
    });
  });

Here all 3 Table Insertion Actions with Callbacks code

let checkDBData = require('./check_data');
let checkUserInfoData = {};
let processError = false;

function registrationSave(regData, connection, callback) {
    checkUserInfoData.colName = 'username';
    checkUserInfoData.colValue = regData.username;
    checkDBData.checkUserInfoFieldsExists(checkUserInfoData, connection, function(err, result) {
        if(err) {
            console.log(err); 
            callback(err, null);
             connection.release();
        }
        else if(!err && result == regData.username){
            callback(null, result);
            connection.release();
        }
        else {
            checkUserInfoData.colName = 'email';
            checkUserInfoData.colValue = regData.email;
            checkDBData.checkUserInfoFieldsExists(checkUserInfoData, connection, function(err, result) {
                if(err) {
                    console.log(err); //do something to setback flow
                    callback(err, null);
                    connection.release();
                }
                else if(!err && result == regData.email){
                    //
                    callback(null, result);
                    connection.release();
                } 
                else {
                    insertRegistration(regData, connection, function(err, result) {
                        callback(err, result);
                        connection.release();
                    });
                }
            });
        }   
    });

    let insertRegistration = function(regData, connection, done) {

        let regStoreData = {
            // here Registration Form Post data is Equalizing with Table columns 
        }; 

        connection.query('INSERT INTO registration SET ?', regStoreData, function(err, result) {
            if (err) {
                console.log(err);
                done(err, null); 
            }
            else {
                let loginTableFilled = false;
                let userinfoTableFilled = false;
                console.log(result);
                insertUserInfo(regData, connection, function(err, response){
                    if(err)
                        done(err, null);
                    else {
                        userinfoTableFilled  = true;
                    } 
                });

                inserLogin(regData, connection, function(err, response){
                    if(err)
                        done(err, null);
                    else {
                        loginTableFilled = true;
                    } 
                });
                if(loginTableFilled && userinfoTableFilled) {
                    done(null,'success')
                }
                //
            }
        });
    }

    let insertUserInfo = function(regData, connection, cb) {
        let userInfoTableData = {
           // here also user_info table column is defining with Post Registration details
        }

        connection.query('INSERT INTO user_info SET ?', userInfoTableData, function(err, result) {
            if (!err && result) {
                console.log(result.affectedRows); 
                cb(null, result.affectedRows);
            }
            else {
                console.log(err);
                cb(err, null); 
            }
        }); 
    }  

    let inserLogin = function(regData, connection, cb) {
        let loginTableData = {
           // here login table is updating from Registration detials
        }

        connection.query('INSERT INTO login SET ?', loginTableData, function(err, result) {
            if (!err && result) {
                console.log(result.affectedRows); 
                cb(null, result.affectedRows);
            }
            else {
                console.log(err);
                cb(err, null); 
            }
        }); 
    }    
}

And regrading the Google, self work above code is the proof that a student tried upto this extent. I have seen Bluebird promises are best solution if we have thought of promise, but my level of coding struck at call back itself.

Any extent help is Appreciated

Community
  • 1
  • 1
hari-j
  • 21
  • 1
  • Guys ... i am waiting .. still ... and i am visiting this page every 6 to 8 hours for any reply .. please help me in any way .. – hari-j Oct 18 '16 at 14:57

0 Answers0