jeremycx / node-LDAP

LDAP binding for node.js
MIT License
221 stars 43 forks source link

Can't bind in connect function as described in Readme #96

Closed jrgleason closed 7 years ago

jrgleason commented 7 years ago

I am trying to do a simple list of all users on a local apacheDS. My code is like this...

var LDAP = require('ldap-client');
var connect = function(){
  ldap.bind({
            binddn: 'uid=admin,ou=system',
            password: 'secret'
  }, function(err) {
    search();
  });
}
var search = function(){
    var search_options = {
      base: "ou=system",
      scope: LDAP.SUBTREE,
      filter: '(objectclass=person)'
    }
    // connected and ready
    ldap.search(search_options, function(err, data){
      if(data == null || data.length === 0) return console.log("Nothing found");
      for(var i = 0; i < data.length; i++){
        console.log(data[i].cn);
      }
    });
}
var ldap = new LDAP({
    uri: 'ldap://localhost:10389',   // string
    connect: connect
}, function(err) {
    console.log(JSON.stringify(ldap));
});

The problem is when I run this I get TypeError: Cannot read property 'bind' of undefined. When I change things a little I can get things working...

var ldap = new LDAP({
    uri: 'ldap://localhost:10389',   // string
}, function(err) {
    console.log(JSON.stringify(ldap));
    connect();
});

So can I just not use the connect attribute of LDAP? Do I need to use this instead of ldap?

jeremycx commented 7 years ago

The callback doesn't currently run in nextTick() on the initial callback (though it arguably should)... Use this instead. I'll update the docs.