使用带有承诺的ldapjs

时间:2015-06-06 12:01:52

标签: javascript node.js promise ldapjs

我想将以下代码转换为使用promise。它正在工作并在活动目录中输出用户的属性。

var client = ldap.createClient({
  url: ldap_url
});

client.bind(ldap_username, ldap_password, function (err) {
    client.search(ldap_dn_search, opts, function (err, search) {
        search.on('searchEntry', function (entry) {
          var user = entry.object;
          // It is working!!!. It outputs all user attributes.
          console.log(user);
        });

    });
}); 

以下是我的尝试,但不输出任何内容。

var Promise = require('promise');
var client_bind = Promise.denodeify(client.bind);
var client_search = Promise.denodeify(client.search);

client_bind(ldap_username, ldap_password)
.then(function(err){
  client_search(ldap_dn_search, opts)
    .then(function(search){
      var search_on = Promise.denodeify(search.on);
      search_on('searchEntry')
        .then(function(entry){
          var user = entry.object;

          // It doesn't output anything !!!
          console.log(user);
        });
      });

    });

4 个答案:

答案 0 :(得分:2)

我遇到了同样的问题。 搜索会发出事件,因此我们需要一些处理它们并在链中进一步传递的东西。 这段代码对我有用:

var reader = new FileReader();
var currentFileType = file.type;
reader.onloadend = function(evt){
    readCompleted(evt,currentFileType);
};
reader.onerror = fail;

在搜索之后,我再添加一个捕获事件的步骤,处理它们,然后沿着链进一步传递它。这使得函数searchPromise。

祝你好运编码)

答案 1 :(得分:0)

这些方法很可能需要在client上作为上下文进行调用,因此在将它们传递给Promise.denodeify之前,您需要先bind()

var client_bind = Promise.denodeify(client.bind.bind(client));
var client_search = Promise.denodeify(client.search.bind(client));

此外,正确使用承诺将如下所示:

client_bind(ldap_username, ldap_password).then(function() {
    return client_search(ldap_dn_search, opts);
//  ^^^^^^ always return something from the callback
}).then(function(search) { // flatten your chain
    return Promise.denodeify(search.on).call(search, 'searchEntry');
//                                    ^^^^^^ an alternative to `bind`
}).then(function(entry){
    var user = entry.object;
    console.log(user);
}).catch(function(err) { // always catch errors!
    console.error(err);
});

答案 2 :(得分:0)

使用Bluebird Promises,简单的方法是正常创建客户端,然后在客户端上运行promisifyAll()。

var ldap = require('ldapjs');
var Promise = require('bluebird');

var client = ldap.createClient({
  url: 'ldap://my-server:1234',
});

Promise.promisifyAll(client);

现在你可以调用client.addAsync()和client.searchAsync()等。

client.bindAsync(secUserDn, secUserPassword)
  .then(doSearch) // if it works, call doSearch
  .catch(function (err) { // if bind fails, handle it
    console.error('Error on bind', err)
  });

function doSearch(data) {
  client.searchAsync('CN=A Test,OU=Users,DC=website,DC=com', options)
    .then(function (data) { // Handle the search result processing
      console.log('I got a result');
    })  
    .catch(function (err) { // Catch potential errors and handle them
      console.error('Error on search', err);
    });
}

答案 3 :(得分:0)

我在这里遇到了同样的问题,但我通过添加 promiseresolve 响应而不使用 bluebird 解决了它,这是一个例子我的代码:

async getLdapUser(username: any): Promise<any> {
    let myPromise = new Promise<boolean>((resolve, reject) => {
      console.log('ssssssssss', username);
      const adSuffix = 'OU=xxxx,OU=xxxxx,DC=xxxxxxx,DC=xxxxxx'; 
      const password = 'xxxxxxxxxxxxx';
      // Create client and bind to AD
      const client = ldap.createClient({
        url: 'ldap://1.1.1.1:389',
      });
      // promise.promisifyAll(client);
      let resp = false;
      // console.log(client);
      client.bind('userTest', password,(err: any) => {
        console.log('RESP', resp);
        if (err) {
          console.log('Error in new connetion ' + err);
        } else {
          /*if connection is success then go for any operation*/
          console.log('Success');
          const searchOptions: {} = {
            scope: 'sub',
            filter: '(sAMAccountName=' + username + ')',
            attributes: ['sAMAccountName'],
          };

          client.search(adSuffix, searchOptions, (err: any, res: any) => {
            assert.ifError(err);
            res.on('searchEntry', (entry: any) => {
              resp = true;

            });
            res.on('error', (error: any) => {
              console.log('err');
              reject(error.message);
            });

            await res.on('end', (result: any) => {
              resolve(resp);

            });
          });
        }
      });
    });
    return myPromise;

  }