SailsJS Waterline与Bluebird Promises

时间:2015-06-05 05:55:32

标签: node.js sails.js waterline

当使用水线ORM时,如果我想使用默认发送的bluebird promise api如何将处理传递回控制器。

以下是代码:

module.exports = {
    //Authenticate
    auth: function (req, res) {
        user = req.allParams();
        //Authenticate
        User.authenticate(user, function (response) {
            console.log(response);
            if (response == true) {
                res.send('Authenticated');
            } else {
                res.send('Failed');
            }
        });
    }
};


module.exports = {
    // Attributes

    // Authenticate a user
    authenticate: function (req, cb) {
        User.findOne({
            username: req.username
        })
        .then(function (user) {
            var bcrypt = require('bcrypt');
            // check for the password
            bcrypt.compare(req.password, user.password, function (err, res) {
                console.log(res);
                if (res == true) {
                    cb(true);
                } else {
                    cb(false);
                }
            });
        })
        .catch(function (e) {
            console.log(e);
        });
    }
};

我只是想尝试实现身份验证功能。业务逻辑很直接。我感到困惑的是,请求流是如何从那时传回控制器的。如果我尝试返回响应,则承诺不响应,但执行cb(value)有效。

1 个答案:

答案 0 :(得分:7)

与承诺合作的关键是永远不要打破链条。承诺链取决于返回承诺或值,或抛出错误的每一步。

以下是您的代码的重写。注意

  • 路径中的每个回调都返回一些内容,每个函数都返回它使用的promise链(甚至.auth();它可能在某些时候有用)
  • 我已经使用BlueBird .promisifyAll()bcrypt一起玩
  • 我通过明确.authenticate()username个参数,将password与您的请求/响应基础架构分离。通过这种方式,它可以更容易地重复使用。

所以现在我们有了(没有100%经过测试,我没有打扰安装水线):

module.exports = {
    // authenticate the login request
    auth: function (req, res) {
        var params = req.allParams();
        return User.authenticate(params.username, params.password)
        .then(function () {
            res.send('Authenticated');
        })
        .fail(function (reason) {
            res.send('Failed (' + reason + ')');
        });
    }
};

var Promise = require("bluebird");
var bcrypt = Promise.promisifyAll(require('bcrypt'));

module.exports = {
    // check a username/password combination
    authenticate: function (username, password) {
        return User.findOne({
            username: username
        })
        .then(function (user) {
            return bcrypt.compareAsync(password, user.password)
        })
        .catch(function (err) {
            // catch any exception problem up to this point
            console.log("Serious problem during authentication", err);
            return false;
        })
        .then(function (result) {
            // turn `false` into an actual error and
            // send a less revealing error message to the client
            if (result === true) {
                return true;
            } else {
                throw new Error("username or password do not match");
            }
        });
    }
};
相关问题