如果我在dataBase.js文件中有这个表:
client.query(
'CREATE TEMPORARY TABLE IF NOT EXISTS '+USER+
'(username VARCHAR(255), '+
'password VARCHAR(255), '+
'name VARCHAR(255), '+
'picture VARCHAR(255), '+
'PRIMARY KEY(username))'
);
并且假设我想检查我的数据库中是否已有给定用户,
运行以下查询后如何获取数据? :
function checkUser(username,password){
client.query('SELECT username, password FROM ' + USER + 'WHERE username=?', [username] , 'AND password=?', [password]
function (err, results, fields) {
if (err) {
throw err;
}
});
}
如果发生错误,将会处理但是如何获取所需数据?
任何帮助将不胜感激!
答案 0 :(得分:2)
如果没有错误,您可以在results
function checkUser(username,password,haveResult) {
client.query('SELECT username, password FROM ' + USER + 'WHERE username=? AND password=?', [username, password],
function (err, results, fields) {
if (err) {
// problems
throw err;
} else {
// do something with data - it is in results array
var checkResult = true; // here something depending on query result
haveResult(checkResult); // continue via callback
}
});
}
// use it
checkUser('bar', 'baz', function(isGood) {
console.log('user is' + (isGood? 'good' : 'bad') );
});