2016-05-28 122 views
0

新建節點/ JS和我創建一個密碼恢復頁面退出的IT門戶,它會搜索AD(LDAP)和其中用戶已註冊的數據庫。基於來自兩個結果呈現選項權威性然後更改PWD節點/ JS承諾阻止

用戶當我提出一個要求($ 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 

攤主似乎是從調用內再回報auth.getUserRegistration(globalUser.sql)後()。來自auth控制器的日誌記錄很快就會記錄來自sql查詢的結果,這正好在它被解析之前,因此返回結果如下()。所以我不相信這個認證控制器(它獨立地工作得很好),而是在resolve()中有問題。也許是因爲它在另一個承諾之內?

我發現了什麼有趣的,混淆我是,如果我從另一個選項卡/瀏覽器的後端工作完成瀏覽到門戶網站和頁面呈現馬上。下面

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

     } 
    }); 
    }); 
} 
+0

我建議你添加一個時間每個日誌語句,並可能添加一些日誌語句來追蹤更詳細的進展,然後你可以清楚地看到,當它發生時所採取的時間。 – jfriend00

+0

純粹用於同步代碼塊的'.then()'處理程序是有點不尋常的。雖然這不是你長時間拖延的原因,但這無助於你的執行速度。 – jfriend00

+0

嗨jfrend,我其實,我剝奪了一大堆調試線,因爲他們沒有提供任何價值。返回auth.getUserRegistration(globalUser.sql)和後面的then()之間沒有任何關係。其他一切按預期返回並記錄。任何特定的地方,你可以推薦登錄?我可以添加時間,但我擔心,他們將只返回了什麼我已經知道 – halfknot

回答

0

我結束了從邊緣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); 
     }); 
    }); 
    }); 
}