与蓝鸟诺言使用ldapjs

我在这里发布类似的东西: 使用ldapjs承诺 。 不幸的是,它仍然没有解决。

这次我尝试了蓝鸟,希望能得到一些好运。

// https://www.npmjs.com/package/ldapjs var Promise = require('bluebird'); var ldap = Promise.promisifyAll( require('ldapjs') ); var config = require('./config'); var print_r = require('print_r').print_r; var my_filter = "(&(objectCategory=person)(objectClass=user)" + "(cn=" + 'someone' + "))"; var ldap_username = config.ad.username; var ldap_password = config.ad.password; var ldap_url = config.ad.url; var ldap_dn_search = config.ad.dn_search; process.env.NODE_TLS_REJECT_UNAUTHORIZED = "0"; ldap.Attribute.settings.guid_format = ldap.GUID_FORMAT_B; var opts = { filter: my_filter, scope: 'sub', }; //test console.log(my_filter); console.log(ldap_username); console.log(ldap_password); console.log(ldap_url); console.log(ldap_dn_search); /* NOTE: This code is working!!! 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; console.log(user); }); }); }); */ // I tried to rewrite the code above with promise ldap.createClientAsync({ url: ldap_url }) .then(function(client){ console.log('bind'); // No print return client.bindAsync(ldap_username, ldap_password); }) .then(function() { console.log('search'); // No print return client.searchAsync(ldap_dn_search, opts); }) .then(function(search) { // No flow here search.on('searchEntry', function (entry) { var user = entry.object; console.log(user); }); }) 

该脚本不输出任何东西。 它正在等待terminal的东西。

       

网上收集的解决方案 "与蓝鸟诺言使用ldapjs"

使用蓝鸟承诺,简单的方法是正常创build客户端,然后在客户端运行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); }); } 

我解决了我自己的问题。 看看这里: http : //kenpeter.github.io/promise/2015/06/13/use-ldapjs-with-promise.html