nodejs查询数据库后,获取result结果集并赋值返回

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

===================================================================================

上一篇:[C++]PAT乙级1001.害死人不偿命的(3n+1)猜想(15/15)


下一篇:PAT乙级 1001. 害死人不偿命的(3n+1)猜想 (15)