My intended use of the code is to check whether the database have a username and password that is passed to the function in the form of object User.
I initialize the success variable to false. And then I say if I find the record, then I will set it to true. However, the success variable is still false even when the record is found. The variable seems not be updating. Why? Also, How can I pass back the whole fetcheduser result while I can indicate whether the record is found or not foun? Many thanks
LoginSuccess : function (User) {
var success = false;
function isEmptyObject(obj) {
return !Object.keys(obj).length;
}
function isEmptyObject(obj) {
for (var key in obj) {
if (Object.prototype.hasOwnProperty.call(obj, key)) {
return false;
}
}
return true;
}
var sql = require('mssql');
var config = require('./configuration/sqlconfig');
var fetcheduser;
sql.connect(config).then(function () {
console.log('Connected to DB');
new sql.Request().query("SELECT * FROM dbo.LocalUser WHERE Username = '" + User.username + "' AND Password = '" + User.password + "';")
.then(function (recordset) {
if (isEmptyObject(recordset)) {
console.log("The User does not exist");
} else {
fetcheduser = JSON.parse(JSON.stringify(recordset));
success = true;
console.log("The user is existed.");
}
}).catch(function (err) {
//When errors come
});
});
return success;
}