所以..。我正在学习如何许诺,这是最难的。
我按照我的建议使用了bluebird
(https://github.com/petkaantonov/bluebird) --以便驯服我一直得到的回调地狱。示例:
function login(req,res,con,mysql,P) {
var ref = undefined;
con.getConnection(function(err,connection) {
if (err) console.log("Get Connection Error.. "+err);
con.query('SELECT password,id FROM player WHERE name='+mysql.escape(req.body.user),function(err,rows,fields) {
if (err) throw err;
if (!rows[0]) {
res.send({
"msg":"Your username and or password was incorrect.",
"flag":true,
"title":": Login Failed"
});
}
if (rows[0].password !== "undefined") {
if (hash.verify(req.body.pass,rows[0].password)) {
req.session.loggedIn = true;
req.session.user = rows[0].id;
ref = new P(rows[0].id,con,req);
res.send({
"msg":"You have logged in!",
"flag":false,
"title":": Logged In"
});
} else {
res.send({
"msg":"Your username and or password was incorrect.",
"flag":true,
"title":": Login Failed"
});
}
}
});
connection.release();
});
console.log(ref);
return ref;
}
它令人讨厌,它充满了回调,并且在查询回调执行完成之前,函数返回ref
。
解决方案:承诺!
我尝试使用快速入门来转换我的函数。因此,我在创建连接之前promisefyAll
了我的mysql
模块:
var mysql = require("mysql");
var Promise = require("bluebird");
Promise.promisifyAll(mysql);
我把我的函数写成如下:
function login(req,res,con,mysql,P) {
var ref = undefined;
Promise.promisify(con.getConnection)().then(function(connection) { //updated line returns error no method promisify. Bluebird v. 1.1.1
con.query('SELECT password,id FROM player WHERE name='+mysql.escape(req.body.user)).then(function(rows,fields) {
if (hash.verify(req.body.pass,rows[0].password)) {
req.session.loggedIn = true;
req.session.user = rows[0].id;
ref = new P(rows[0].id,con,req);
res.send({
"msg":"You have logged in!",
"flag":false,
"title":": Logged In"
});
} else {
res.send({
"msg":"Your username and or password was incorrect.",
"flag":true,
"title":": Login Failed"
});
}
});
return ref;
});
}
但是我一直在找TypeError: Cannot call method 'then' of undefined at Object.login (/var/www/post/posts.js:36:22)
和
TypeError: undefined is not a function at Pool.<anonymous> (/var/www/node_modules/mysql/lib/Pool.js:53:14)
错误。有人能帮我理解如何(正确)实现查询我的数据库的承诺吗?
Edit (post answer acceptance:):下面是我调用登录函数的方式:
app.post("/login",function(req,res) {
Player = post.login(req,res,con,mysql,p);
console.log(Player); // logs [Object object]
}); //inside the login function, it logs the object like it should
发布于 2014-03-26 03:09:20
当您终止原型时,promise返回方法将具有*Async后缀
promisification的想法是假装这个库一开始就是为了返回承诺而设计的。您不应该在运行时在应用程序代码中调用promisify,而应该在应用程序引导程序初始化代码或类似代码中调用promisify。
var mysql = require("mysql");
var Promise = require("bluebird");
//Only need to be called once per application so probably not here
Promise.promisifyAll(require("mysql/lib/Connection").prototype);
Promise.promisifyAll(require("mysql/lib/Pool").prototype);
function login(req,res,con,mysql,P) {
return con.getConnectionAsync().then(function(connection) {
return connection.queryAsync('SELECT password,id FROM player WHERE name='+
mysql.escape(req.body.user)).spread(function(rows, fields) {
if (hash.verify(req.body.pass,rows[0].password)) {
req.session.loggedIn = true;
req.session.user = rows[0].id;
var ref = new P(rows[0].id,con,req);
res.send({
"msg":"You have logged in!",
"flag":false,
"title":": Logged In"
});
return ref;
} else {
res.send({
"msg":"Your username and or password was incorrect.",
"flag":true,
"title":": Login Failed"
});
}
}).finally(function() {
connection.release();
});
});
}
未来的版本将有更好的资源管理,您将能够做到:
function login(req,res,con,mysql,P) {
return Promise.using(con.getConnectionAsync(), function(connection) {
return connection.queryAsync('SELECT password,id FROM player WHERE name='+
mysql.escape(req.body.user));
}).spread(function(rows, fields) {
if (hash.verify(req.body.pass,rows[0].password)) {
req.session.loggedIn = true;
req.session.user = rows[0].id;
var ref = new P(rows[0].id,con,req);
res.send({
"msg":"You have logged in!",
"flag":false,
"title":": Logged In"
});
return ref;
} else {
res.send({
"msg":"Your username and or password was incorrect.",
"flag":true,
"title":": Login Failed"
});
}
});
}
如何使用结果:
app.post("/login",function(req,res) {
post.login(req,res,con,mysql,p).then(function(Player) {
});
})
https://stackoverflow.com/questions/22643061
复制相似问题