简体   繁体   English

Node / JS承诺被阻止

[英]Node / JS promises blocked

New to Node/JS and I'm creating a password recovery page for an exiting IT portal, it searches AD(ldap) and a DB where the user has registered. 这是Node / JS的新功能,我正在为一个现有的IT门户创建密码恢复页面,它会搜索AD(ldap)和用户已注册的数据库。 Based on the results from both presents user with options to auth then change pwd 根据两者的结果,用户可以选择进行身份验证,然后更改密码

When I make a request ($get()) from the client to find a user the backend stalls intermittently. 当我从客户端发出请求($ get())来查找用户时,后端会间歇性地停顿。 See console logging below 请参阅下面的控制台日志记录

This is the console logging 这是控制台日志记录

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.
.

This is where time is spent doing who knows what. 这是花时间做谁知道的地方。 It eventually completes but usually takes minutes. 最终完成,但通常需要几分钟。 then prints... 然后打印...

in sql results

The stall seems to be after calling return auth.getUserRegistration(globalUser.sql) from within a then(). 停滞似乎是在then()内部调用return auth.getUserRegistration(globalUser.sql)之后。 The logging from the auth controller very quickly logs the results from the sql query which is right before it should be resolved, thereby returning the results to the following then(). 来自auth控制器的日志记录非常迅速地记录了来自sql查询的结果,该结果恰好在应解决之前,因此将结果返回到随后的then()。 So i don't believe there to be a issue with the auth controller (which independently works flawlessly) but rather within the resolve(). 所以我不相信auth控制器(它可以完美地独立工作)不会有问题,而是在resolve()中存在问题。 Perhaps because its within a another promise?. 也许因为它在另一个承诺之内?

What I find Interesting and confuses me is if I browse to the portal from another tab/browser the backend work completes and the 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)
  }

});

/* 
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);

      }
    });
  });
}

I ended up changing my db module from edge-sql to mssql and updated my code accordingly. 我最终将数据库模块从edge-sql更改为mssql,并相应地更新了我的代码。 It worked immediately. 它立即起作用。

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);
        });
    });
  });
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM