我正在尝试在我的node.js代码中使用Ldap.js的搜索方法。这是我的客户端代码。它成功添加了一个用户,但搜索新添加的用户没有任何结果。( ldap服务器在https://github.com/osixia/docker-openldap的docker容器中运行)
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);
}
});此外,如果有用的话,下面是我通过运行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显示ldap中的所有用户时新添加的用户的样子。
# userId7, example.org
dn: cn=userId7,dc=example,dc=org
cn: userId7
userPassword:: cGFzc3dvcmQ=
objectClass: person
sn: efub更新:我可以使用外壳命令docker exec ldap-service ldapsearch -LLL -x -D "cn=admin,dc=example,dc=org" -w "admin" -b "cn=userId7,dc=example,dc=org" "(objectclass=*)"成功地搜索到用户"userId7“。如何才能使ldapJS也成功地运行此搜索?
更新2:我也可以通过使用前台"phpLDAPadmin“成功搜索,如下截图所示:


发布于 2020-12-15 15:00:57
所以我解决了这个问题。正确的client.search代码是:
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");
}
});
}
);在function (error, res) {内部,我通过client.on("searchEntry"而不是res.on("searchEntry"监听事件,因此从搜索结果中遗漏了事件。根本原因是一个典型的复制和粘贴错误,以及在误解事件来源时更改变量。
https://stackoverflow.com/questions/65285808
复制相似问题