javascript - 如何从 activedirectory 方法中返回值
问题描述
我在一个查询 ActiveDirectory 的类中有一个方法。因此,我使用的是“activedirectory2”npm 包。我成功通过身份验证并将结果成功记录到控制台。
现在我已经实例化了我的类并尝试调用该方法,但我无法获得非空结果。
我尝试使用 getter/setter 以在实例化类后使 _result 值可用。我试图通过研究异步调用来解决我的问题,但显然无法提出正确的问题。
类活动目录
var ActiveDirectory = require("activedirectory2");
class AuthenticateWithLDAP {
constructor(user, password){
this._result = [];
this.user = user;
this.password = password;
this.config = {
url: "ldaps://someldap",
baseDN: "somebasdn",
username: this.user,
password: this.password,
filter: 'somefilter',
}
this.ad = new ActiveDirectory(this.config);
}
//Auth Method
auth() {
var result = this._result;
this.config.entryParser = function(entry,raw,callback){
if(entry.hasOwnProperty('info')) {
result.push(entry.info);
this._result = result;
}
callback(entry);
}
this.ad.authenticate(config.username, config.password, (err,auth)=>{
if (err) {
//some error handling
}
if (auth) {
this.ad.find(config,async (err, userDetails) => {
var result = this._result;
{
if (err) {
//some error handling
}
if(!userDetails) {
console.log("No users found.");
} else {
this._result = result[0]; //I want this result!
console.log('result: ', this._result);
return await this._result;
}
}
})
} else {
console.log("Authentication failed!");
}
});
}
//getter/setter
get result(){
return this._result;
}
set result(value) {
this._result.push(value);
}
}
module.exports = AuthenticateWithLDAP;
路由模块
const express = require('express');
const AuthwithLDAP = require('AuthenticateWithLDAP');
const router = express.Router();
router.post('/', async (req,res,next) => {
let x = async ()=> {
authwithldap = new AuthwithLDAP(req.body.user,req.body.password);
return await authwithldap.auth();
}
x().then((res)=>{
console.log('res: ', res); //always []
})
})
我希望能够在我的 router.post 方法处理程序中使用 AuthenticateWithLDAP 类的 _result 值。实际上我只在 router.post 中得到 [] (空数组)。
您能否告诉我如何以某种方式更改值 _result,以便类的实例知道它并可以在类本身之外使用它。
非常感谢。
米查
解决方案
我不是 100% 确定,但我认为这应该可行。在您的代码中,您无法返回结果,因为返回是在回调中。有办法解决这个问题。
- 将回调传递给
auth()
方法(这很糟糕,因为回调很糟糕) - 返回一个承诺并解决结果
我决定去兑现承诺。
var ActiveDirectory = require("activedirectory2");
class AuthenticateWithLDAP {
constructor(user, password){
this._result = [];
this.user = user;
this.password = password;
this.config = {
url: "ldaps://someldap",
baseDN: "somebasdn",
username: this.user,
password: this.password,
filter: 'somefilter',
}
this.ad = new ActiveDirectory(this.config);
}
//Auth Method
auth() {
return new Promise((resolve, reject) => {
this.ad.authenticate(config.username, config.password, (err,auth)=>{
if (err) {
//Call reject here
}
if (auth) {
this.ad.find(config,async (err, userDetails) => {
var result = this._result;
{
if (err) {
//some error handling
}
if(!userDetails) {
console.log("No users found.");
} else {
this._result = result[0]; //I want this result!
resolve(await this._result);
}
}
})
} else {
console.log("Authentication failed!");
}
});
});
}
}
module.exports = AuthenticateWithLDAP;
const express = require('express');
const AuthwithLDAP = require('AuthenticateWithLDAP');
const router = express.Router();
router.post('/', async (req,res,next) => {
/* This code can be simplifed
let x = async () => {
authwithldap = new AuthwithLDAP(req.body.user,req.body.password);
return await authwithldap.auth();
}
x().then((res)=>{
console.log('res: ', res); //always []
})
*/
(async () => {
authwithldap = new AuthwithLDAP(req.body.user,req.body.password);
var res = await authwithldap.auth();
console.log('res: ', res);
})();
})
推荐阅读
- java - 拥有多个按钮会使我的应用程序崩溃
- angular - 重新加载recaptcha2 Angular 6
- django - Django rest 框架 - 基于单个类的视图绑定到多个 url
- python - Flask:将 requests.get API 数据保存到 sqlite 数据库
- spring-cloud-stream - 未找到适用于 kafka 的 Spring Cloud Stream 应用程序图像查看器
- javascript - 不知道我的代码有什么问题
- javascript - 如何在我的循环中编写“if”条件得到语法错误并更新列表中的一行
- c - malloc/struct 指针数组段错误
- linux - 剪切和 tr:找不到命令
- laravel - 连接到有 Homestead 问题的 Sequel Pro