admin管理员组文章数量:1328355
app.post('/login', function (req, res) {
var login = req.body.login;
var pass = req.body.pass;
var ret = CheckUserValid(login, pass);
res.send(ret);
})
function CheckUserValid(login, pass,callback) {
var sql = "SELECT * FROM `user` WHERE login = ? AND pass= ?";
client.query(sql, [login, pass], function selectResutl(err, results, fields) {
console.log(results);
if (!err) return true;
else
throw err;
});
}
first Function is about request and second is about making call to mysql. because it's async so it's not worked. Can someone let me know how I can make it work synchronously like in C#.
app.post('/login', function (req, res) {
var login = req.body.login;
var pass = req.body.pass;
var ret = CheckUserValid(login, pass);
res.send(ret);
})
function CheckUserValid(login, pass,callback) {
var sql = "SELECT * FROM `user` WHERE login = ? AND pass= ?";
client.query(sql, [login, pass], function selectResutl(err, results, fields) {
console.log(results);
if (!err) return true;
else
throw err;
});
}
first Function is about request and second is about making call to mysql. because it's async so it's not worked. Can someone let me know how I can make it work synchronously like in C#.
Share Improve this question asked May 31, 2012 at 20:19 ankitankit 973 silver badges12 bronze badges 1-
5
If you're going to use NodeJS, you might as well learn to write asynchronous code. The
CheckUserValid
function accepts a callback, so why not use it? – user1106925 Commented May 31, 2012 at 20:28
3 Answers
Reset to default 4The signature of your CheckUserValid
method already implies a callback
.
You can use that to call something when your db-request is done, like this:
app.post('/login', function (req, res) {
var login = req.body.login;
var pass = req.body.pass;
// use a closure here for the callback of checkUserValid
checkUserValid(login, pass, function(err, isValid) {
// send the response when checkUserValid is done
res.send(isValid)
});
})
function checkUserValid(login, pass, callback) {
var sql = "SELECT * FROM `user` WHERE login = ? AND pass= ?";
client.query(sql, [login, pass], function(err, results, fields) {
console.log(results);
if (!err) {
// there is no error, pass null for the error
// and true for the result
callback(null, true);
} else {
// an error occurred in the db-query
// pass it to the callback
callback(err)
}
});
}
By convention you should pass the err
as first argument, but in the end that's up to you.
However you can pass the original error up to your initial function and handle it there.
You should a look at Fibers module.
You can chain the functions that you want to call in order
e.g. you can make res.send(ret);
as the callback of your CheckUserValid
本文标签: javascripthow to make Nodejs call synchronous then I can use it easilyStack Overflow
版权声明:本文标题:javascript - how to make Nodejs call synchronous then I can use it easily? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742259805a2442301.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论