【问题标题】:undefined is not a function while writing a promise function in Nodejs在Nodejs中编写promise函数时未定义不是函数
【发布时间】:2016-11-15 06:38:49
【问题描述】:

我正在尝试使用 Bluebird Promise Library 编写我的函数。 我通过以下方式向 ldap-js 承诺了 ldap-js 的 createClient 函数:

    var Promise= require('bluebird'); //done at the beginning
        var createClientAsync = Promise.promisify(require('ldapjs').createClient);
getUser:function(user) {
    var memberRoles = [];
    var searchFilter = '(&(member='+user.dn+'))';
     var opts = {
           filter: searchFilter,
           scope: 'sub',
           attributes: ['dn']
         };

    createClientAsync({
           url: 'ldap://x.x.x.x:3889'
         })
         .then(function(client){
           return client.search('o=pic', opts);
         })
         .then(function(res) {
           res.on('searchEntry', function(entry) {
               console.log('entry: ' + JSON.stringify(entry.object));
               for (var role in roles) {
                 var mapping = roles[role];
                 if (mapping.group === entry.object.dn) {
                   memberRoles.push(role);
                 }
               }
             });
         })
         .then(function() {
             return memberRoles;
         });
}

我在 createClientAsync 处收到错误,未定义不是函数。

【问题讨论】:

  • requirebluebird 了吗?
  • @VsevolodGoloviznin 是的。我做了 Promise= require('bluebird');
  • 你能把整个代码贴出来吗?
  • @VsevolodGoloviznin 已更新
  • getUser 返回undefined,因为它在任何地方都没有返回值——即使你修复了这个问题,promise 链也会解析为一个空数组,因为倒数第二个 .then 不会将任何东西推入 memberRoles在最后的 .then 之前返回 memberRoles ...

标签: javascript node.js promise bluebird


【解决方案1】:

简要阅读ldapjs文档后,我可以建议以下代码

getUser:function(user) {
    var searchFilter = '(&(member='+user.dn+'))';
    var opts = {
        filter: searchFilter,
        scope: 'sub',
        attributes: ['dn']
    };
    return createClientAsync({
        url: 'ldap://x.x.x.x:3889'
    })
    .then(function(client){
        return client.search('o=pic', opts);
    })
    .then(function(res) {
        var memberRoles = [];
        return new Promise(function(resolve, reject) {
            res.on('searchEntry', function(entry) {
                console.log('entry: ' + JSON.stringify(entry.object));
                for (var role in roles) {
                    var mapping = roles[role];
                    if (mapping.group === entry.object.dn) {
                        memberRoles.push(role);
                    }
                }
            });
            res.on('end', function() {
                resolve(memberRoles);
            });
        });
    });
}

注意“new Promise”和 res.on('end' 以在“搜索”完成后解决该承诺

正如我所说,简要阅读文档,所以这可能完全无效:p

【讨论】:

  • 在不知道roles 是什么的情况下,假设:p
  • @JaromandaX 谢谢,我会检查并更新。目前我正在通过以下方式调用getUser:user.roles = this.getUser(user); this.xyz(user,'test').then(function(res) { .. }).catch(function(e) { .. });函数 xyz 会在 getUser 完成之前执行还是我需要将这些承诺链接在一起,例如: this.getUser(user) .then(function(res) { user.roles = res; }).then(this.xyz ) 等等?
  • 如果你想在执行this.xyz之前等待getUser“完成”,那么,是的,你需要链接它
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2016-02-03
  • 2018-12-16
  • 1970-01-01
  • 1970-01-01
  • 2019-03-24
  • 2020-10-03
  • 1970-01-01
相关资源
最近更新 更多