module.exports from promise

时间:2017-12-28 17:06:28

标签: javascript node.js

我如何将函数initPassport从turbo.then()分配给module.exports?

function turbo() {
    return new Promise(function (resolve, reject) {
        UsersModel.find({
            userName: 'bumblebee'
        }, function (err, userMongo) {
            userMongo.forEach(function (one) {
                resolve({
                    username: one.userName,
                    password: one.password
                });
            });
        });
    });
}

turbo.then(function (data) {
    var user = data;

    function initPassport() {
        passport.use(new LocalStrategy(
            function (username, password, done) {
                findUser(username, function (err, user) {
                    if (err) {
                        return done(err);
                    }
                    if (!user) {
                        return done(null, false);
                    }
                    if (password !== user.password) {
                        return done(null, false);
                    }
                    return done(null, user);
                });
            }
        ));
        passport.authenticationMiddleware = authenticationMiddleware;
    }
});

module.exports = ??????????;

module.exports = initPassport;不行。告诉我,我应该如何重新编写此代码?

2 个答案:

答案 0 :(得分:4)

这可以通过多种方式实现,我将在下面重点介绍。

方法#1

从行开始移动匿名函数:

turbo.then(function (data) {

要附加在turbo函数内:

function turbo() {
    return new Promise(function (resolve, reject) {
     ...
    })
    .then(function (data) {
     ...
    });
}

然后module.exports可能会设为turbo

方法#2

将匿名函数移动为命名定义,例如:

function authentication(data) {
    var user = data;

    function initPassport() {
        passport.use(new LocalStrategy(
            function (username, password, done) {
                findUser(username, function (err, user) {
                    if (err) {
                        return done(err);
                    }
                    if (!user) {
                        return done(null, false);
                    }
                    if (password !== user.password) {
                        return done(null, false);
                    }
                    return done(null, user);
                });
            }
        ));
        passport.authenticationMiddleware = authenticationMiddleware;
    }
}

然后module.exports可以设置为:

module.exports = function () {
    return turbo().then(authentication);
};

答案 1 :(得分:0)

将涡轮移动到主要功能涡轮中。将其重命名为.then

function turbo() {
return new Promise(function (resolve, reject) {
    UsersModel.find({
        userName: 'bumblebee'
    }, function (err, userMongo) {
        userMongo.forEach(function (one) {
            resolve({
                username: one.userName,
                password: one.password
            });
        });
    });
})
.then(function (data) {
var user = data;

function initPassport() {
    passport.use(new LocalStrategy(
        function (username, password, done) {
            findUser(username, function (err, user) {
                if (err) {
                    return done(err);
                }
                if (!user) {
                    return done(null, false);
                }
                if (password !== user.password) {
                    return done(null, false);
                }
                return done(null, user);
            });
        }
    ));
    passport.authenticationMiddleware = authenticationMiddleware;
   }
  });
}