我试图用ldapjs执行过滤器取决于第一次搜索结果的搜索
ldapClient.search(base1, opts1, (err1, res1) => {
res1.on("searchEntry", entry => {
const myObj = { attr1: entry.object.attr1 }
const opts2 = { filter: entry.object.filter }
if (entry.object.condition == myCondition) {
ldapClient.search(base2, opts2, (err2, res2) => {
res2.on("searchEntry", entry => {
myObj.attr2 = entry.object.attr2
});
});
}
console.log(myObj);
});
});
问题是,当console.log最终显示我的对象时,事件" .on"我的第二次搜索尚未被捕获。
那么,在显示对象之前,如何告诉我的代码等待第二个事件完成?
由于
答案 0 :(得分:0)
执行class A: UIViewController {
@IBOutlet weak var collectionView: UICollectionView!
}
class B: A {
//Should be empty right? since it inherits from A,
// but the collectionView from super class is not initialized.
}
由于异步行为,您需要等待第二次搜索完成。
你要把它放在“.on”中:
console.log(myObj)
也适用于优雅代码您可以像以下一样使用它:
ldapClient.search(base1, opts1, (err1, res1) => {
res1.on("searchEntry", entry => {
let myObj = { attr1: entry.object.attr1 }
let opts2 = { filter: entry.object.filter }
if (entry.object.condition == myCondition) {
ldapClient.search(base2, opts2, (err2, res2) => {
res2.on("searchEntry", entry => {
myObj.attr2 = entry.object.attr2
console.log("res2", myObj);
});
});
return;
}
console.log("res1", myObj);
});
});
答案 1 :(得分:0)
谢谢num8er。
我终于使用了"承诺的ldap"模块,基于ldapjs和promises
ldapClient.bind(dn, password).then(() => {
let p;
ldapClient.search(base1, opts1).then(res1 => {
const entry = res1.entries[0].object;
const myObj = { attr1: entry.attr1 };
if (entry.condition == myCondition) {
const opts2 = { filter: entry.filter }
p = ldapClient.search(base2, opts2).then(res2 => {
const entry2 = res2.entries[0].object;
myObj.attr2 = entry2.attr2;
return myObj;
});
} else {
p = Promise.resolve(myObj);
}
p.then(value => console.log("Obj", value);
});
});