jeremycx / node-LDAP

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

Reconnect Function #84

Closed webCoderLOL closed 8 years ago

webCoderLOL commented 8 years ago

I have problem to understand the reconnect function. I looked at the index.js but saw no one is calling it. I think I must miss something. Therefore, I couldn't get my code working as following:

================= NOT WORKING =================================

var LDAP = require('ldap-client');
var ldap = new LDAP({
    uri: 'ldap://localLDAP',
    base: 'DC=example,DC=com',
    scope: LDAP.SUBTREE,
        reconnect: function(){
        // bind
        ldap.bind({
        binddn: 'cn=User,DC=example,DC=com',
        password: 'pw'
            }, function(err){
            if(err)
            console.log(err);
            }
        });
}, function(err){
    if(err)
        console.log(err);
});
// search
ldap.search({
    attrs: 'telephoneNumber',
    filter: 'cn=User'
}, function(err, data){
    if(err)
        console.log("search error: "+err);
    // console.log(JSON.stringify(data, null, 2));
    console.log("telephoneNumber: "+data[0].telephoneNumber)
    ldap.close();
});

However, the following code is working

===================== Awesome =====================

var LDAP = require('ldap-client');
var ldap = new LDAP({
    uri: 'ldap://localLDAP',
    base: 'DC=example,DC=com',
    scope: LDAP.SUBTREE
}, function(err){
    if(err)
        console.log(err);
});

// bind
ldap.bind({
    binddn: 'cn=User,DC=example,DC=com',
    password: 'pw'
}, function(err){
    if(err)
        console.log(err);

    // search
    ldap.search({
        attrs: 'telephoneNumber',
        filter: 'cn=User'

    }, function(err, data){
        if(err)
            console.log("search error: "+err);
        // console.log(JSON.stringify(data, null, 2));
        console.log("telephoneNumber: "+data[0].telephoneNumber)

        ldap.close();
    });
});
jeremycx commented 8 years ago

Documentation error :(

Try connect: instead of reconnect:

webCoderLOL commented 8 years ago

Thanks for the quick reply. I tried that too but i got

tim-m1:ldap tim$ node ldapClient.js 
/Users/tim/apps/ldap/ldapClient.js:9
        ldap.bind({
            ^

TypeError: Cannot read property 'bind' of undefined
    at Object.LDAP.connect (/Users/tim/apps/ldap/ldapClient.js:9:7)
    at LDAP.onconnect (/Users/tim/apps/ldap/node_modules/ldap-client/index.js:104:25)
    at new LDAP (/Users/tim/apps/ldap/node_modules/ldap-client/index.js:99:33)
    at Object.<anonymous> (/Users/tim/apps/ldap/ldapClient.js:3:12)
    at Module._compile (module.js:425:26)
    at Object.Module._extensions..js (module.js:432:10)
    at Module.load (module.js:356:32)
    at Function.Module._load (module.js:313:12)
    at Function.Module.runMain (module.js:457:10)
    at startup (node.js:138:18)
jeremycx commented 8 years ago

Can't remember the calling context for that -- what does this.bind() do?

webCoderLOL commented 8 years ago

Here is the code that complained ldap.bind()

var LDAP = require('ldap-client');

var ldap = new LDAP({
    uri: 'ldap://localLDAP',
    base: 'DC=example,DC=com',
    scope: LDAP.SUBTREE,
    connect: function(){
        // bind
        ldap.bind({
            binddn: 'cn=User,DC=example,DC=com',
            password: 'pw'
        }, function(err){
            if(err)
                console.log(err);

            // search
            ldap.search({
                attrs: 'telephoneNumber',
                filter: 'employeeNumber=503433'

            }, function(err, data){
                if(err)
                    console.log("search error: "+err);
                // console.log(JSON.stringify(data, null, 2));
                console.log("telephoneNumber: "+data[0].telephoneNumber)

                ldap.close();
            });
        });
    }
}, function(err){
    if(err)
        console.log(err);
});
jeremycx commented 8 years ago

Right, try replacing ldap.bind() in the connect function to be this.bind()

webCoderLOL commented 8 years ago

Good point. I tried that too.

TypeError: this.bind is not a function
jeremycx commented 8 years ago

Fixed in 3.1.3 Sorry for the long delay.

osallou commented 7 years ago

README example still refers to ldap.bind instead of this.bind