0

I am trying to write a function of mine using Bluebird promise Library. I promisified the ldap-js the createClient function of ldap-js by:

    var Promise= require('bluebird'); //done at the beginning
        var createClientAsync = Promise.promisify(require('ldapjs').createClient);
getUser:function(user) {
    var memberRoles = [];
    var searchFilter = '(&(member='+user.dn+'))';
     var opts = {
           filter: searchFilter,
           scope: 'sub',
           attributes: ['dn']
         };

    createClientAsync({
           url: 'ldap://x.x.x.x:3889'
         })
         .then(function(client){
           return client.search('o=pic', opts);
         })
         .then(function(res) {
           res.on('searchEntry', function(entry) {
               console.log('entry: ' + JSON.stringify(entry.object));
               for (var role in roles) {
                 var mapping = roles[role];
                 if (mapping.group === entry.object.dn) {
                   memberRoles.push(role);
                 }
               }
             });
         })
         .then(function() {
             return memberRoles;
         });
}

I get an error at createClientAsync undefined is not a function.

user1692342
  • 5,007
  • 11
  • 69
  • 128

1 Answers1

0

After a brief reading of the ldapjs documentation, I can suggest the following code

getUser:function(user) {
    var searchFilter = '(&(member='+user.dn+'))';
    var opts = {
        filter: searchFilter,
        scope: 'sub',
        attributes: ['dn']
    };
    return createClientAsync({
        url: 'ldap://x.x.x.x:3889'
    })
    .then(function(client){
        return client.search('o=pic', opts);
    })
    .then(function(res) {
        var memberRoles = [];
        return new Promise(function(resolve, reject) {
            res.on('searchEntry', function(entry) {
                console.log('entry: ' + JSON.stringify(entry.object));
                for (var role in roles) {
                    var mapping = roles[role];
                    if (mapping.group === entry.object.dn) {
                        memberRoles.push(role);
                    }
                }
            });
            res.on('end', function() {
                resolve(memberRoles);
            });
        });
    });
}

note the "new Promise" and res.on('end' to resolve the promise once the "search" has completed

as I said, brief reading of documentation, so this may be completely invalid :p

Jaromanda X
  • 53,868
  • 5
  • 73
  • 87
  • Probably the [`for…in` enuration should be a proper array iteration](http://stackoverflow.com/q/500504/1048572) – Bergi Nov 14 '16 at 23:28
  • without knowing what `roles` is, that would be assuming :p – Jaromanda X Nov 14 '16 at 23:29
  • @JaromandaX Thanks, I will check it out and update. Currently I am calling, getUser in the following manner: user.roles = this.getUser(user); this.xyz(user,'test').then(function(res) { .. }).catch(function(e) { .. }); Will the function xyz get executed before getUser is completed or do I need to chain these promises together, like: this.getUser(user) .then(function(res) { user.roles = res; }).then(this.xyz ) and so on ? – user1692342 Nov 15 '16 at 00:10
  • if you want to wait for getUser to "complete" before executing this.xyz, then, yes, you need to chain it – Jaromanda X Nov 15 '16 at 00:12