On my server side (Node.js), I have a file that calls a function (ldapjs) in another file but the returned value is undefined. The true value of client.bind is then displayed but it is not returned to the first file.
Result of test.js
But I need to return "ok" from ldap.js -- > return('ok)
test2.js
const f = require('./ldap3');
console.log(f.ldap_conn());
ldap3.js
var ldap = require('ldapjs');
var client = ldap.createClient({url: 'ldap://ldap-pr.tsl.bobo.com:389'});
const dn = 'uid=myname,ou=people, ou=teamMembers,ou=internal,o=bobo'
const password = 'mypassword'
function ldap_conn(){
client.bind(dn, password, function(error){
if(error){
console.log('fail')
return('not ok')
}
else{
console.log('not fail')
return('ok')
}
})
}
//ldap_conn()
//console.log(test)
module.exports = {ldap_conn};
Result of test.js undefined not fail
test2.js should receive "ok" but it receives "undefined". But console.log('not fail') works afterwards and this value is not returned.
In short, client.bind work fine but the value is not returned.
ok
and not ok
aren't the return values of the bind
function, but of the callback function.
One approach is to have the caller pass their own callback:
// ldap3.js
var ldap = require('ldapjs');
var client = ldap.createClient({url: 'ldap://ldap-pr.tsl.bobo.com:389'});
const dn = 'uid=myname,ou=people, ou=teamMembers,ou=internal,o=bobo'
const password = 'mypassword'
function ldap_conn(callback) {
client.bind(dn, password, callback);
}
module.exports = {ldap_conn};
// test3.js
const f = require('./ldap3');
f.ldap_conn(function (error) {
if (error) {
console.log('not ok');
} else {
console.log('ok');
}
});