0

Good Morning All,

I have been looking for an answer to this on the boards, but my noob brain just can't make sense of it.

i have this function in models/user.js

module.exports.getUserByUsername = function(username, callback){
  var retUser = new User;
  sql.connect(dbConfig, function(err) {
    if (err) {
      console.log(err);
      callback();
    }

    // create Request object
    var request = new sql.Request();
    request.input('ip_username', sql.NVarChar, username)

    // query to the database and get the records
    request.query('select * from [portal_users] where username = @ip_username', function(err, recordset) {
      if (err) {
        console.log(err);
        return;
      } else {

        var user = new User(recordset.recordset[0].username,recordset.recordset[0].password,recordset.recordset[0].email,recordset.recordset[0].name);
        user.addID(recordset.recordset[0].id);
        retUser = user;

      }
      callback();


      // send records as a response
      //res.send(recordset);

    });
    });

    function callback() {
     sql.close();
      return retUser;
    };
    }

and this code in my routes/user.js

passport.use(new LocalStrategy(
function(username, password, done) {
 User.getUserByUsername(username, function(err, user){

if(err) throw err;
if(!user){
    return done(null, false, {message: 'Unknown User'});
}

User.comparePassword(password, user.password, function(err, isMatch){
    if(err) throw err;
    if(isMatch){
        return done(null, user);
    } else {
        return done(null, false, {message: 'Invalid password'});
    }
});
});
}));

I have been modifying an example from GITHUB that uses mongoDB for the DB connection, but I would like to use MS SQL. The function is successfully calling the database and returning the correct values. However I don't know how to initiate the callback so I can pass the retUser object back to the original function for processing and logging in.

I did for a moment try to do this by not using the callback and using a standard return type of function, however I quickly realised that given the async nature this wouldn't work.

any help here would be greatly appreciated.

Thanks

Stevo_300
  • 321
  • 5
  • 14

1 Answers1

0

OK I managed to figure it out using this post: Node.js npm mssql function returning undefined

my new code is:

module.exports.getUserByUsername = function(username, callback){


  var connection =  new sql.ConnectionPool(dbConfig, function(err) {
    if (err) {
      console.log(err);
      callback(err);
      return
    }

    // create Request object
    var request = new sql.Request(connection);
    request.input('ip_username', sql.NVarChar, username)

    // query to the database and get the records
    request.query('select * from [portal_users] where username = @ip_username', function(err, recordset) {
      if (err) {
        console.log(err);
        callback(err,recordset);
        return;
      } else {

        var user = new User(recordset.recordset[0].username,recordset.recordset[0].password.replace(/ /g,''),recordset.recordset[0].email,recordset.recordset[0].name);
        user.addID(recordset.recordset[0].id);
        callback(err,user);
      }
      sql.close();

      // send records as a response
      //res.send(recordset);

    });
});
}
Stevo_300
  • 321
  • 5
  • 14