如何从Active Directory方法中返回值

时间:2019-04-30 14:58:07

标签: javascript node.js active-directory

我在类中确实有一个查询ActiveDirectory的方法。 因此,我正在使用'activedirectory2'npm软件包。 我已成功通过身份验证并将结果成功记录到控制台。

现在我已经实例化了我的课程并尝试调用该方法,但我无法获得非空结果。

在使类保持不变之后,我尝试使用getters / 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值,以便该类的实例知道它并可以在类本身之外使用它。

非常感谢您。

Micha

2 个答案:

答案 0 :(得分:0)

我不确定100%,但是我认为这应该可行。 在您的代码中,您无法返回结果,因为返回是在回调中。 有一些解决方法。

  1. 将回调传递给auth()方法(这很糟糕,因为回调很烂)
  2. 兑现承诺,并最终解决问题

我已经决定要承诺。

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

答案 1 :(得分:0)

您可以尝试添加这样的语法“ await”吗?

await x().then((res)=>{
  console.log('res: ', res); //always []
})

由于您的“ x”方法处于异步模式,也许您必须等待Promise得以解决...

相关问题