简体   繁体   中英

NodeJS - LDAPJS Error : events.js:160 throw er; // Unhandled 'error' event

all I am new to nodejs and I am facing this issue while executing my node file. I am on centos 7. When I am trying to connect to LDAP using LDAPJS. It works fine when LDAP is available but then it cannot connect to server it show this weird error :

events.js:160 throw er; // Unhandled 'error' event ^

Error: connect EHOSTUNREACH 172.24.130.203:389 at Object.exports._errnoException (util.js:1020:11) at exports._exceptionWithHostPort (util.js:1043:20) at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1099:14)

I want to catch this error and show a proper error message.

I have tried using this :

 rm -rf node_modules && npm cache clean --force && npm install

But it did not help.

My package.json file is :

{
  "name": "package",
  "version": "1.0.0",
  "description": "For LDAP ",
  "main": "index.js",
  "dependencies": {
    "ldap": "^0.7.1",
    "passport": "^0.4.0"
  },
  "devDependencies": {},
  "scripts": {
    "test": "node index.js"
  },
  "keywords": [
    "ldapjs"
  ],
  "author": "Rajan",
  "license": "ISC"
}

And this is index.js file :

var ldap = require('ldapjs');
var http = require('http');
var assert = require('assert');


var client = ldap.createClient({
    url: 'ldap://172.24.130.203/dc=india,dc=bizrtc,dc=com'
});

var basedn = "dc=test,dc=test,dc=test";

var opts = {
  filter: '(ipPhone=*)',
  scope: 'sub',
  attributes: ['ipPhone','mail','name','description']
};

client.bind('username', 'mysecret', function (err) {

client.search('dc=india,dc=bizrtc,dc=com', opts, function (err, search) {
    search.on('searchEntry', function (entry) {
      var user = entry.object;
      console.log("Users are " + user.ipPhone + "  Display Name is  " + user.name + " E-Mail is " + user.mail + " Password is  " + user.description);

    });

  });
});


client.unbind(function(err) {
  assert.ifError(err);
});

As it seems, the client catchs socket errors and re-emits these: Client - error

So you should be fine with something like

client.on('error', err => { console.error(err) })

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM