查询实施
var connection = mysql.createConnection({
host: 'localhost',
user: 'root',
password: '密码',
database: '数据库'
});
connection.connect();
exports.selectByUsername = selectByUsername;
function selectByUsername(username){
var promise = new Promise(function(resolve){
var sql = "SELECT * FROM user WHERE username = ?";
var sqlParams = [username];
connection.query(sql,sqlParams,function(err,result){
if(err){
return console.log(err.message);
}
console.log("------------------------开始查询---------------------");
console.log(result);
var str = JSON.stringify(result);
var json = JSON.parse(str);
console.log("------------------------查询结束---------------------");
resolve(str);
});
// connection.end();
});
promise.then(function(value){
console.log(value);
return value;
});
return promise;
}
调用
var promise =exports.selectByUsername(req.body.username);;
promise.then(function(value){
console.log(value);
if(value!=null){
// req.session.user = value;
// req.session.save();
res.end(value);
}else{
res.end(null);
}
});