ldapjs / node-ldapjs

LDAP Client and Server API for node.js
http://ldapjs.org
MIT License
1.61k stars 445 forks source link

How can I determine whether the entry is found? #25

Closed BYVoid closed 12 years ago

BYVoid commented 13 years ago

I was developing a project that use ldapjs as a ldap client. But now I found that I can not receive a message when the entry was not found by using client.search. For example, when I search '(uid=nuk)' which exists in the ldap server, the callback function of res.on('searchEntry',callback); would be called, and the results of

res.on('end',function(result){
    console.log('status: '+result.status);
});

is "status: 0".

But when I search '(uid=nuk1)', which does not exist, result.status is also 0. Even the whole object of 'result' is the same as the one when the entry is found.

Now I have not an approach to get a message when the entry is not found. The version I was using is 0.2.0

mcavage commented 13 years ago

Hi. Is this a "standard" LDAP server, or a server you wrote using ldapjs?

I ask because doing a search underneath a tree that does exist will always return LDAP_SUCCESS (0) as the status code. However, you just won't get any matching entries. So, that's important to realize that you can't disambiguate an entry's existence when doing a subtree search on the return code. You could do a search scope of base (but you then have to know the DN), and then, iirc, you would get back NO_SUCH_OBJECT if it does not exist. That's of course assuming the LDAP server you're talking to responds that way, but most do.

Now, in your example I assume you have an .on('searchEntry', function(entry) {...}); in that code? That's the only way you can use a "successful" ldap search request to know whether or not there were entries. I.e., something like:

var opts = {
  ilter: '(uid=nuk)',
  scope: 'sub'
};
client.search('dc=foo', opts, function(err, res) {
  assert.ifError(err);

  var entries = [];
  res.on('searchEntry', function(entry) {
    entries.push(entry.object);
  });
  res.on('error', function(err) {
    console.error('error: ' + err.message);
  });
  res.on('end', function(result) {
    console.log('status=%d, num_entries=%d', result.status, entries.length);
    console.log('entries => %j', entries);
  });
});