NodeJS ActiveDirectory2 findusers回调函数问题

wribegjk  于 2023-04-20  发布在  Node.js
关注(0)|答案(1)|浏览(90)

下面是我的代码:

import ActiveDirectory from 'activedirectory2';
..........
let config = {
    attributes: { user: ["*"] },
    url: myAD,
    baseDN: 'dc=xx,dc=yy,dc=com',
    username: 'username@yy.com',
    password: 'password'
}
let missingPostList = [];
let ad = new ActiveDirectory(config); 
let staffUtil = new StaffUtil();
let staffList = await staffUtil.getStaffList();

for (let i = 0; i < staffList.length; i++) {
    staff = staffList[i];
    let query = "title=" + staff.post;
    ad.findUsers(query, function(err, users){

        if (err) {
            console.log('ERROR: ' + JSON.stringify(err));
            return;
        }
        if ((!users) || (users.length == 0)) {
            //console.log('There are no colleague title is ' + staff.post + '.');
            missingPostList.push(staff.post);
        }else {
          ...................
        }
    });
}
console.log(missingPostList);

当我启用以下语句时,

console.log('There are no colleague title is ' + staff.post + '.');

程序显示有2篇文章在Active Directory中不存在。
不幸的是,当我禁用该语句时,它只输出一个空数组[]。
在我的程序中,如果没有staff posts满足输入,则应执行以下语句。

missingPostList.push(staff.post);

这意味着missingPostList数组应该被修改。然而,程序只输出一个空数组,为什么数组没有任何值?我如何解决这个问题?

7cjasjjr

7cjasjjr1#

最后,我找到了解决方案:

import ActiveDirectory from 'activedirectory2';
..........
let config = {
    attributes: { user: ["*"] },
    url: myAD,
    baseDN: 'dc=xx,dc=yy,dc=com',
    username: 'username@yy.com',
    password: 'password'
}
let missingPostList = [];
let ad = new ActiveDirectory.promiseWrapper(config); 
let staffUtil = new StaffUtil();
let staffList = await staffUtil.getStaffList();

for (let i = 0; i < staffList.length; i++) {
    staff = staffList[i];
    let query = "title=" + staff.post;
    try{
        let users=await ad.findUsers(query);
        if ((!users) || (users.length === 0)) {
            console.log('There are no colleague title is ' + staff.post + '.');
            missingPostList.push(staff.post);
        }else {
            ..............
        }
    }catch (error){
        console.log('ERROR: ' + JSON.stringify(err));
        break;
    }
}
console.log(missingPostList);

根据ActiveDirectory2的文档,

A Promise wrapper is available for all methods by an alternate require statement.

下面的语句是ES6格式的:

let ad = new ActiveDirectory.promiseWrapper(config);

相关问题