Khi sử dụng đường nước ORM, nếu tôi muốn tiêu thụ các api lời hứa bluebird vận chuyển theo mặc định làm thế nào để tôi vượt qua việc xử lý trở lại bộ điều khiển.SailsJS Waterline với Bluebird Promises
Dưới đây là các mã:
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);
});
}
};
Tôi chỉ đơn giản là cố gắng để thực hiện một chức năng xác thực. Logic kinh doanh là thẳng về phía trước. Những gì tôi đang nhầm lẫn là làm thế nào dòng chảy yêu cầu được trao lại cho bộ điều khiển kể từ đó. Lời hứa không đáp ứng nếu tôi cố gắng trả lời một câu trả lời, nhưng làm một cb (giá trị) hoạt động.
đâu bạn trở Promise trong mã này? – vanadium23
Theo tài liệu (http://sailsjs.org/#!/documentation/reference/waterline/queries), waterline có một phần thực hiện của Bluebird, tôi đang có gói đầu của tôi xung quanh mã sau khi User.find .. ..then –