节点/JS 承诺被阻止
Node / JS promises blocked
Node/JS 的新手,我正在为现有的 IT 门户创建密码恢复页面,它搜索 AD(ldap) 和用户已注册的数据库。根据两者的结果,用户可以选择进行身份验证,然后更改 pwd
当我从客户端发出请求($get())以查找用户时,后端会间歇性地停止。请参阅下面的控制台日志记录
这是控制台日志记录
Attempting to find : <account name>
Found AD user: <account name>
in acc prep
in sql request
in getUserRegistration //within auth controller
.
.here it prints the results of the sql query // At this point I would expect the promise to be fulfilled.
.
这就是花时间做谁知道的事情的地方。它最终会完成,但通常需要几分钟。然后打印...
in sql results
似乎是在从 then() 中调用 return auth.getUserRegistration(globalUser.sql) 之后。来自 auth 控制器的日志记录非常快速地记录了 sql 查询的结果,该查询恰好在它应该被解析之前,因此 return 将结果传递给下面的 then()。所以我不认为 auth 控制器(独立完美地工作)有问题,而是在 resolve() 中。也许是因为它在另一个承诺中?
我觉得有趣但让我感到困惑的是,如果我从另一个 tab/browser 浏览到门户网站,后端工作会完成并且页面会立即呈现。
见下方代码
/* 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)
}
});
/*
this is the route that renders based the work from 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 to 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(",")
for (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 a record exist in a DB based on results from ad.getUser
requires edge/edge-sql
*/
auth.getUserRegistration = function(accounts) {
return new Promise(function(resolve, reject) {
var query = "select * from [users] where (username = "
query += "\'" + accounts.username + "\'"
if (accounts.domain2UPN) {
query += " or domain2UPN = \'" + accounts.domain2UPN + "\'"
}
if (accounts.domain1UPN) {
query += " or domain1UPN = \'" + accounts.domain1UPN + "\'"
}
query += ") and q1col is not null and q2col is not null and pin is 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);
}
});
});
}
我最终将我的数据库模块从 edge-sql 更改为 mssql 并相应地更新了我的代码。立竿见影。
auth.getUserRegistration = function(accounts) {
return new Promise(function(resolve, reject) {
var query = "select * from [users] where (username = "
query += "\'" + accounts.username + "\'"
if (accounts.domain2UPN) {
query += " or domain2UPN = \'" + accounts.domain2UPN + "\'"
}
if (accounts.domain1UPN) {
query += " or domain1UPN = \'" + accounts.domain1UPN + "\'"
}
query += ") and q1col is not null and q2col is not null and pin is 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);
});
});
});
}
Node/JS 的新手,我正在为现有的 IT 门户创建密码恢复页面,它搜索 AD(ldap) 和用户已注册的数据库。根据两者的结果,用户可以选择进行身份验证,然后更改 pwd
当我从客户端发出请求($get())以查找用户时,后端会间歇性地停止。请参阅下面的控制台日志记录
这是控制台日志记录
Attempting to find : <account name>
Found AD user: <account name>
in acc prep
in sql request
in getUserRegistration //within auth controller
.
.here it prints the results of the sql query // At this point I would expect the promise to be fulfilled.
.
这就是花时间做谁知道的事情的地方。它最终会完成,但通常需要几分钟。然后打印...
in sql results
似乎是在从 then() 中调用 return auth.getUserRegistration(globalUser.sql) 之后。来自 auth 控制器的日志记录非常快速地记录了 sql 查询的结果,该查询恰好在它应该被解析之前,因此 return 将结果传递给下面的 then()。所以我不认为 auth 控制器(独立完美地工作)有问题,而是在 resolve() 中。也许是因为它在另一个承诺中?
我觉得有趣但让我感到困惑的是,如果我从另一个 tab/browser 浏览到门户网站,后端工作会完成并且页面会立即呈现。
见下方代码
/* 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)
}
});
/*
this is the route that renders based the work from 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 to 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(",")
for (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 a record exist in a DB based on results from ad.getUser
requires edge/edge-sql
*/
auth.getUserRegistration = function(accounts) {
return new Promise(function(resolve, reject) {
var query = "select * from [users] where (username = "
query += "\'" + accounts.username + "\'"
if (accounts.domain2UPN) {
query += " or domain2UPN = \'" + accounts.domain2UPN + "\'"
}
if (accounts.domain1UPN) {
query += " or domain1UPN = \'" + accounts.domain1UPN + "\'"
}
query += ") and q1col is not null and q2col is not null and pin is 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);
}
});
});
}
我最终将我的数据库模块从 edge-sql 更改为 mssql 并相应地更新了我的代码。立竿见影。
auth.getUserRegistration = function(accounts) {
return new Promise(function(resolve, reject) {
var query = "select * from [users] where (username = "
query += "\'" + accounts.username + "\'"
if (accounts.domain2UPN) {
query += " or domain2UPN = \'" + accounts.domain2UPN + "\'"
}
if (accounts.domain1UPN) {
query += " or domain1UPN = \'" + accounts.domain1UPN + "\'"
}
query += ") and q1col is not null and q2col is not null and pin is 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);
});
});
});
}