我打破了我的承诺

所以..我最难学习如何承诺。

我使用bluebird ( https://github.com/petkaantonov/bluebird )build议给我 – 为了驯服我的回拨地狱我已经越来越。 例:

 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; } 

它是讨厌的,它充满了callback,并且在查询callback完成执行之前函数返回ref

解决scheme:承诺!

我尝试使用快速启动转换我的function..所以我promisefyAll '我的mysql模块创build连接之前:

 var mysql = require("mysql"); var Promise = require("bluebird"); Promise.promisifyAll(mysql); 

我写下我的function如下:

 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)

错误。 有人可以帮我理解如何实现查询我的数据库的承诺(正确)?

编辑(接受答复接受:):这里是我如何调用loginfunction:

 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 

当你promisify一个原型,承诺返回的方法将*asynchronous后缀

promisification的想法是假装图书馆的devise是要回到承诺开始。 您不应在运行时在应用程序代码中调用promisify,而应在您的应用程序引导程序init代码或类似代码中调用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(); }); }); } 

未来的版本将有更好的资源pipe理,你将能够做到:

 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) { }); })