使用 Promise api 和 Node Mysql 不工作
using Promise api with Node Mysql not working
我可以使用以下代码运行 sql 从 MySql 数据库中查询和获取记录
var mySql = require("mysql");
var connection = mySql.createConnection({
host : "localhost",
user : "root",
password : "rahul",
database : "testDb" //schema
});
connection.connect();
connection.query("select * from departmentTbl",function(err,rows,fields){
if(err){
console.log(err.stack);
}
for(var i = 0; i < rows.length; i++){
console.log("DepartmentId : \'"+rows[i]["deptId"]+"\', DepartmentName \'"+rows[i]["deptName"]+"\'");
}
});
connection.end();
但是如果我使用 A+ Promise api A+ Promise API
,我无法 运行 代码
如下图,
var Promise = require("promise");
var mySql = require("mysql");
var connection = mySql.createConnection({
host : "localhost",
user : "root",
password : "rahul",
database : "testDb" //schema
});
var p = Promise.resolve(connection.connect());
var sqlQuery = p.then(function(con){
return Promise.resolve(con.query);
});
sqlQuery.then(function(q){
q("select * from departmentTbl").then(function(err,row,fields){
if(err){
console.log(err.stack);
}
for(var i = 0; i < rows.length; i++){
console.log("DepartmentId : \'"+rows[i]["deptId"]+"\', DepartmentName \'"+rows[i]["deptName"]+"\'");
};
}).done(function(){
connection.end();
});
});
请告诉我哪里出错了,为什么我无法解析 Promise。
我的代码甚至没有抛出错误
我能够使用以下实现解决 mysql Node 和 PromiseJS,
var Promise = require("promise");
var mySql = require("mysql");
var connection = mySql.createConnection({
host : "localhost",
user : "root",
password : "rahul",
database : "testDb" //schema
});
connection.connect();
var getDepartments = function(){
return new Promise(function(resolve,reject){
connection.query("select * from departmentTbl",function(err,rows,fields){
if(err){
return reject(err);
}else{
return resolve(rows);
}
}); // query
}); // Promise
} // getDepartments
getDepartments().then(function(rows){
for(var i = 0; i < rows.length; i++){
console.log("DepartmentId : \'"+rows[i]["deptId"]+"\', DepartmentName \'"+rows[i]["deptName"]+"\'");
}
}).catch(function(e){
console.log(e.stack);
});
connection.end();
我可以使用以下代码运行 sql 从 MySql 数据库中查询和获取记录
var mySql = require("mysql");
var connection = mySql.createConnection({
host : "localhost",
user : "root",
password : "rahul",
database : "testDb" //schema
});
connection.connect();
connection.query("select * from departmentTbl",function(err,rows,fields){
if(err){
console.log(err.stack);
}
for(var i = 0; i < rows.length; i++){
console.log("DepartmentId : \'"+rows[i]["deptId"]+"\', DepartmentName \'"+rows[i]["deptName"]+"\'");
}
});
connection.end();
但是如果我使用 A+ Promise api A+ Promise API
,我无法 运行 代码如下图,
var Promise = require("promise");
var mySql = require("mysql");
var connection = mySql.createConnection({
host : "localhost",
user : "root",
password : "rahul",
database : "testDb" //schema
});
var p = Promise.resolve(connection.connect());
var sqlQuery = p.then(function(con){
return Promise.resolve(con.query);
});
sqlQuery.then(function(q){
q("select * from departmentTbl").then(function(err,row,fields){
if(err){
console.log(err.stack);
}
for(var i = 0; i < rows.length; i++){
console.log("DepartmentId : \'"+rows[i]["deptId"]+"\', DepartmentName \'"+rows[i]["deptName"]+"\'");
};
}).done(function(){
connection.end();
});
});
请告诉我哪里出错了,为什么我无法解析 Promise。
我的代码甚至没有抛出错误
我能够使用以下实现解决 mysql Node 和 PromiseJS,
var Promise = require("promise");
var mySql = require("mysql");
var connection = mySql.createConnection({
host : "localhost",
user : "root",
password : "rahul",
database : "testDb" //schema
});
connection.connect();
var getDepartments = function(){
return new Promise(function(resolve,reject){
connection.query("select * from departmentTbl",function(err,rows,fields){
if(err){
return reject(err);
}else{
return resolve(rows);
}
}); // query
}); // Promise
} // getDepartments
getDepartments().then(function(rows){
for(var i = 0; i < rows.length; i++){
console.log("DepartmentId : \'"+rows[i]["deptId"]+"\', DepartmentName \'"+rows[i]["deptName"]+"\'");
}
}).catch(function(e){
console.log(e.stack);
});
connection.end();