带有 Bluebird Promise 的 SailsJS 水线 [英] SailsJS Waterline with Bluebird Promises
问题描述
当使用水线 ORM 时,如果我想使用默认提供的 bluebird promise api,如何将处理传递回控制器.
When using the waterline ORM, if I want to consume the bluebird promise api thats shipped by default how to I pass the processing back to the controller.
代码如下:
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) 有效.
I am simply trying to implement a authentication function. The business logic is straight forward. What I am confused of is how the request flow is handed back to the controller since. The promise doesn't respond if I try to return a response, but doing a cb(value) works.
推荐答案
使用 Promise 的关键是永远不要打破链条.承诺链取决于每一步,要么返回承诺或值,要么抛出错误.
The key to working with promises is to never break the chain. A promise chain depends on every step either returning a promise or a value, or throwing an error.
以下是您的代码的重写.请注意
The following is a rewrite of your code. Note that
- 路径中的每个回调都返回一些东西,每个函数都返回它所使用的承诺链(甚至
.auth()
;它在某些时候可能有用) - 我使用 BlueBird 的
.promisifyAll()
使bcrypt
一起玩 - 通过使
username
和password
参数显式,我已将.authenticate()
与您的请求/响应基础结构分离.这样可以更轻松地重复使用.
- Every callback in the path returns something and every function returns the promise chain it works with (even
.auth()
; it might be useful at some point) - I've used BlueBird's
.promisifyAll()
to makebcrypt
play along - I've decoupled
.authenticate()
from your request/response infrastructure by making theusername
andpassword
arguments explicit. This way it can be reused more easily.
所以现在我们有了(不是 100% 测试,我没有费心安装水线):
So now we have (not 100% tested, I did not bother installing waterline):
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");
}
});
}
};
这篇关于带有 Bluebird Promise 的 SailsJS 水线的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!