javascriptnode.jsldapopenldapldapjs

Search in ldapjs


I am trying to use the search method of Ldap.js in my node.js code. Here is my code for the client side. It adds successfully a user, but searching for the newly added user does not yield any results. (The ldap server is running in a docker container from https://github.com/osixia/docker-openldap)

var ldap = require("ldapjs");
var assert = require("assert");

var client = ldap.createClient({
  url: "ldap://localhost:389",
});

client.bind("cn=admin,dc=example,dc=org", "admin", function (err) {
  assert.ifError(err);
  let newUser = {
    cn: "userId7",
    userPassword: "password",
    objectClass: "person",
    sn: "efub",
  };
  // Here i successfully add this user "userId7"
  client.add(
    "cn=userId7,dc=example,dc=org",
    newUser,
    (err, response) => {
      if (err) return console.log(err);
      return response;
    }
  );

  var options = {
    filter: "(objectClass=*)",
    scope: "sub",
  };
  // Now the search, it runs without error, but does never receive a searchEntry
  client.search(
    "cn=userId7,dc=example,dc=org",
    options,
    function (error, search) {
      console.log("Searching.....");

      client.on("searchEntry", function (entry) {
        console.log("I found a result in searchEntry");
      });

      client.on("error", function (error) {
        console.error("error: " + error.message);
      });

      client.unbind(function (error) {
        if (error) {
          console.log(error.message);
        } else {
          console.log("client disconnected");
        }
      });
    }
  );
});

client.on('error', function (err) {
  if (err.syscall == "connect") {
    console.log(err);
  }
});

Also, if it helps, this is how the newly added user looks like when i display all users from ldap by running docker exec my-openldap-container ldapsearch -x -H ldap://localhost:389 -b dc=example,dc=org -D "cn=admin,dc=example,dc=org" -w admin

# userId7, example.org
dn: cn=userId7,dc=example,dc=org
cn: userId7
userPassword:: cGFzc3dvcmQ=
objectClass: person
sn: efub

Update: I can successfully search for the user "userId7" with the shell command: docker exec ldap-service ldapsearch -LLL -x -D "cn=admin,dc=example,dc=org" -w "admin" -b "cn=userId7,dc=example,dc=org" "(objectclass=*)". How can i make ldapJS also run this search successfully?

Update 2: I can also successfully search by using the frontend "phpLDAPadmin" as seen in the screenshots below: search mask search results


Solution

  • So i solved it. The correct client.search code is:

      client.search(
        "cn=userId7,dc=example,dc=org",
        options,
        function (error, res) {
          console.log("Searching.....");
    
          res.on("searchEntry", function (entry) {
            console.log("I found a result in searchEntry", JSON.stringify(entry.object));
          });
    
          res.on("error", function (error) {
            console.error("error: " + error.message);
          });
    
          client.unbind(function (error) {
            if (error) {
              console.log(error.message);
            } else {
              console.log("client disconnected");
            }
          });
        }
      );
    

    Inside function (error, res) { I listened for the events via client.on("searchEntry", instead of res.on("searchEntry", therefore missing the events from the search results. The root cause was a classic copy and paste error and changing the variable while misunderstanding the origin of the event.