javascript - Node / JS promises blocked -
new node/js , i'm creating password recovery page exiting portal, searches ad(ldap) , db user has registered. based on results both presents user options auth change pwd
when make request ($get()) client find user backend stalls intermittently. see console logging below
this console logging
attempting find : <account name> found ad user: <account name> in acc prep in sql request in getuserregistration //within auth controller . .here prints results of sql query // @ point expect promise fulfilled. .
this time spent doing knows what. completes takes minutes. prints...
in sql results
the stall seems after calling return auth.getuserregistration(globaluser.sql) within then(). logging auth controller logs results sql query right before should resolved, thereby returning results following then(). don't believe there issue auth controller (which independently works flawlessly) rather within resolve(). perhaps because within promise?.
what find interesting , confuses me if browse portal tab/browser backend work completes , page renders immediately.
see code below
/* client request */ $.get(urlqry, function(result) { if (result.match("<div id=\"pop\">")) { $.modal(result, { onclose: function () { $("[id^=simplemodal").remove(); $("#findload").hide(); $("#forgetbtn").show(); $.modal.close(); } }); } else { console.log("in else"); $("#divid").html(result) } }); /* route renders based work 2 different controllers 1. check if user exists in active directory (ldap) 2. check if user has registered (sql) */ router.get('/forgetpwd', function(req, res) { var usrname = req.query.usr var domain = req.query.realm if (usrname && domain) { console.log("attempting find : " + usrname + "@" + domain); var globaluser = { username: "", upn: "", accounts: [], sql: {}, registered: false } ad.getuser(usrname, domain) .then(function(result) { if (result.userprincipalname) { console.log("found ad user: " + result.userprincipalname); globaluser.username = result.samaccountname globaluser.upn = result.userprincipalname globaluser.accounts = result.extensionattribute15 } else { var fail = { status: "fail", message: "unknown error" } var msg = result ? result : fail throw msg; } }) .then(function(result) { console.log("in acc prep"); globaluser.sql.username = globaluser.username var upns = globaluser.accounts if (upns) { upns = upns.split(",") (u in upns) { var upn = upns[u].split("@") var domain = upn.slice(-1)[0].tolowercase() switch (domain) { case "domain1": globaluser.sql.domain1upn = upns[u] break; case "domain2": globaluser.sql.domain2upn = upns[u] break; case "domain3": globaluser.sql.ihupn = upns[u] break; default: globaluser.accounts = "" break; } } } else { globaluser.accounts = "" } return; }) .then(function(result) { console.log("in sql request"); return auth.getuserregistration(globaluser.sql) }) .then(function(result) { console.log("in sql results"); if (result.length > 0) { globaluser.registered = true return } else { globaluser.registered = false return } }) .then(function(result) { var accounts = { username: globaluser.username, accounts: globaluser.accounts.split(","), registered: globaluser.registered } console.log(accounts); res.render('./forget-options', accounts); }) .catch(function(error) { console.log("in catch"); var failure = { status: "fail", message: error } console.log(failure); res.render("./ack", failure); return }) } else { console.log("user field empty and/or domaim no selected"); } }); /* checks if a record exist in db based on results ad.getuser requires edge/edge-sql */ auth.getuserregistration = function(accounts) { return new promise(function(resolve, reject) { var query = "select * [users] (username = " query += "\'" + accounts.username + "\'" if (accounts.domain2upn) { query += " or domain2upn = \'" + accounts.domain2upn + "\'" } if (accounts.domain1upn) { query += " or domain1upn = \'" + accounts.domain1upn + "\'" } query += ") , q1col not null , q2col not null , pin not null;" var sqlparams = { connectionstring: "data source=name.domain.com;initial catalog=dbname;integrated security=true", source: query }; var getreg = edge.func('sql', sqlparams); getreg(null, function(error, result) { if (error) { console.log("retrieving user ?? registration"); reject(error); } else { console.log("in getuserregistration"); console.log(result); resolve(result); } }); }); }
i ended changing db module edge-sql mssql , updated code accordingly. worked immediately.
auth.getuserregistration = function(accounts) { return new promise(function(resolve, reject) { var query = "select * [users] (username = " query += "\'" + accounts.username + "\'" if (accounts.domain2upn) { query += " or domain2upn = \'" + accounts.domain2upn + "\'" } if (accounts.domain1upn) { query += " or domain1upn = \'" + accounts.domain1upn + "\'" } query += ") , q1col not null , q2col not null , pin not null;" sql.connect("mssql://<usr>:<pwd>@<server>/<db>").then(function() { new sql.request().query(query).then(function(recordset) { console.log(recordset); resolve(recordset); }).catch(function(err) { console.log(err); }); }).catch(function(err) { console.log(err); }); }); }); }
Comments
Post a Comment