ldapjs 高级搜索子树

问题描述

我在我的项目中使用 ldapjs 库和标准 LDAP 服务器,我正在尝试使用 search()。它可以正常工作,直到我想返回结果。

所以我相信我更多地误解了 javascript 的工作原理,而不是库,因为它的工作正常 console.log

其次我不确定我是否正确有效地使用了嵌套 search()。

任何帮助将不胜感激

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=*)`,};
           client.search(`${entry.object.dn}`,opts2,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}`,function (entry2) {
              phones.push({ ...entry.object,detail: entry2.object });
            });
          });

在这里我无法访问手机阵列,或者没有任何东西被推入其中 所以我必须这样做:

    client.search(`${entry.object.dn}`,function (entry2) {
            });
            phones.push({ ...entry.object,detail: entry2.object });
          });

在这里我无法访问 entry2 :-(

现在失去理智

解决方法

我不熟悉这个 API,但看起来你的代码应该是这样的:

function getPhones() {
  return new Promise((resolve,reject) => {
    let phones = [];
    const opts = {
      filter: `(objectClass=Phone)`,scope: "sub"
    };
    client.search(`cn=${callserver.cn},cn=Modules`,opts,function (err,res) {
      if (err) {
        console.log("Error in promise",err);
      }
      res.on("searchEntry",function (entry) {
        const opts = {
          filter: `(objectClass=*)`,scope: "sub"
        };
        client.search(`${entry.object.dn}`,res) {
          res.on("searchEntry",function (entry2) {
            phones.push({
              ...entry.object,...{
                details: entry2.object
              }
            });
          });
        });
        res.on("end",function () {
          resolve(phones);
        });
        res.on("err",function () {
          reject("Error");
        });
      });
    });
  });
}

以下代码的问题:

client.search(`${entry.object.dn}`,opts2,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

是不是JS执行client.search()是一个异步动作,不等待响应继续执行console.log(newPhone);

所以这里的解决方法是在成功回调中,当响应返回时,简单地将手机推送到结果中。


旁注:如果您想编写“看起来”同步的代码,您还可以查看 async await

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...