I am querying my sqlite3 database to check user details, then once the query has completed it calls a call-back method. Inside the call-back I can access the data just fine, but when I want to make a copy of that data i.e. assign the value to a session variable or a global instance, then it becomes undefined once the call-back has finished. My question here is, how do I avoid this?
var custFuncs = require( '../functions' );
var arrRow = []
// process log in.
router.post( '/login', upload.array(), function( req, res )
{
custFuncs.checkAuthDetails( req.body.uname, req.body.pass, function ( row )
{
//req.session.userID = row.id;
arrRow.push( row.id );
console.log( arrRow ); // PRINTS OUT [ 1 ] HERE
} );
console.log( arrRow ); // PRINTS OUT [] HERE
res.redirect( '/' );
});
Then in functions.js:
function checkAuthDetails( userName, pass, callback )
{
var hashedPass = encrypt( pass );
db.serialize( function()
{
db.get( "SELECT id, uname, pass FROM user WHERE uname = ?", [userName], function( err, row )
{
if( (row.id > 0) && (row.pass == hashedPass) )
{
callback( row );
}
});
});
}