nodejs获取了查询结果,但不能返回出去,
情形如下:
var query = function (path,id,param,sqlWhere,res){
var aa = 111;
var sql = "select * from user";
var mysql = dbHelper.getMysql();
mysql.query({
sql: sql
}, function (err, rows) {
res.end(JSON.stringify(rows));
aa = rows;
});
return aa;//返回结果是111而不是rows
};
解决方式
var query = function (path,id,param,sqlWhere,res){
var promise = new Promise(function (resolve, reject) { var sql = getFileSql(path,id,param,sqlWhere);
var mysql = dbHelper.getMysql();
mysql.query({
sql: sql
}, function (err, rows) {
res.end(JSON.stringify(rows));
resolve(rows);//关键
}); });
promise.then(function (value) {
console.log(value);
return value;
}, function (value) {});
return promise;
};
完毕:
=====================================原文内容如下======================================
http://www.jianshu.com/p/6e77d6fdaf13
===================================================================================