【发布时间】:2021-06-11 04:03:51
【问题描述】:
我正在为我的项目使用 ldapjs 库和标准 LDAP 服务器,并且我正在尝试使用 search()。在我想要返回结果之前,它可以正常工作。
所以我相信这更多的是我对 javascript 工作原理的误解,而不是库作为其工作正常的 console.log
其次,我不确定我是否正确有效地使用了嵌套搜索()。
任何帮助将不胜感激
function getPhones() {
return new Promise((resolve, reject) => {
let phones = [];
const opts = {
filter: `(objectClass=Phone)`,
scope: 'sub',
// attributes: ['*'],
};
client.search(`cn=${callserver.cn},cn=Modules`, opts, function (err, res) {
if (err) {
console.log('Error in promise', err);
}
res.on('searchEntry', function (entry) {
let newPhone = {};
const opts2 = {
filter: `(objectClass=*)`,
scope: 'sub',
};
client.search(`${entry.object.dn}`, opts2, function (err, res) {
res.on('searchEntry', function (entry2) {
newPhone = entry2.object;
console.log(newPhone); //here its logging just fine with all attributes
});
});
console.log(newPhone);// here newPhone is empty
phones.push(
{ ...entry.object, details: newPhone }
// followMeTo: entry.object.followMeTo,
// telephoneNumber: parseInt(entry.object.telephoneNumber),
);
});
res.on('end', function () {
resolve(phones);
});
res.on('err', function () {
reject('Error');
});
});
}
}
更新 1: 如果我尝试按照建议使用:
client.search(`${entry.object.dn}`, opts, function (err, res) {
res.on('searchEntry', function (entry2) {
phones.push({ ...entry.object, detail: entry2.object });
});
});
在这里我无法访问电话数组,或者没有任何东西被推入其中 所以我必须这样做:
client.search(`${entry.object.dn}`, opts, function (err, res) {
res.on('searchEntry', function (entry2) {
});
phones.push({ ...entry.object, detail: entry2.object });
});
但在这里我无法访问 entry2 :-(
现在想不通
【问题讨论】:
-
"但在这里我无法访问 entry2" 您是否收到“
phonesisundefined错误?如果没有,则在此处定义。问题可能是我们在所有电话被推送之前解决了电话的承诺。我无法分辨,因为我不知道 API 是如何工作的,看起来你正试图在网络套接字连接周围放置一个承诺包装器。我假设res.on("end"仅在每个searchEntry完成时才被调用 -
看来你需要让第一个
client.search(cn=`等待第二个client.search(${entry`
标签: javascript node.js asynchronous node-async ldapjs