I have a sqlite3 js controller function for node.js :
exports.findUser=function findUser(user){
var temp ;
var db = new sqlite3.Database('kitchen.db');
var stmt_user_find = "SELECT * FROM user WHERE un = ?";
db.all(stmt_user_find,user,save);
function save(err, rows) {
if (err) throw err;
console.log(rows);
temp = rows;
}
db.close();
return temp;
}
and another function in router using Express:
app.post('/login', function (req, res){
var un=req.body.un;
var pw=req.body.pw;
console.log('router '+db.findUser(un));
});
The result of console is :
router undefined
[ { un: 'as', em: 'sss', pw: 'sss' } ]
The second function aims to get the value from sqlite database, but the result from console shows that the 'return temp' statement is executed firstly and return to the second function, and later the inner function (save) of findUser function is executed. So why it is like this and how can I get value from database?
Thank you, I have tried for hours in this problem.