javascript – node-mysql时序
作者:互联网
我有这样的递归查询(注意:这只是一个例子):
var user = function(data)
{
this.minions = [];
this.loadMinions = function()
{
_user = this;
database.query('select * from users where owner='+data.id,function(err,result,fields)
{
for(var m in result)
{
_user.minions[result[m].id] = new user(result[m]);
_user.minions[result[m].id].loadMinions();
}
}
console.log("loaded all minions");
}
}
currentUser = new user(ID);
for (var m in currentUser.minions)
{
console.log("minion found!");
}
这不起作用,因为timmings都是错误的,代码不等待查询.
我试过这样做:
var MyQuery = function(QueryString){
var Data;
var Done = false;
database.query(QueryString, function(err, result, fields) {
Data = result;
Done = true;
});
while(Done != true){};
return Data;
}
var user = function(data)
{
this.minions = [];
this.loadMinions = function()
{
_user = this;
result= MyQuery('select * from users where owner='+data.id);
for(var m in result)
{
_user.minions[result[m].id] = new user(result[m]);
_user.minions[result[m].id].loadMinions();
}
console.log("loaded all minions");
}
}
currentUser = new user(ID);
for (var m in currentUser.minions)
{
console.log("minion found!");
}
但他暂时冻结,我错过了什么?
解决方法:
解决问题的第一个障碍是理解Node.js中的I / O是异步的.一旦你知道这是如何适用于你的问题,递归部分将更容易(特别是如果你使用流控制库,如Async或Step).
下面是一个示例,它会执行您尝试执行的操作(减去递归).就个人而言,我会避免递归加载可能未知数量/深度的记录;而是按需加载它们,如下例所示:
var User = function(data) {
this.data = data
this.minions;
};
User.prototype.getMinions = function(primaryCallback) {
var that = this; // scope handle
if(this.minions) { // bypass the db query if results cached
return primaryCallback(null, this.minions);
}
// Callback invoked by database.query when it has the records
var aCallback = function(error, results, fields) {
if(error) {
return primaryCallback(error);
}
// This is where you would put your recursive minion initialization
// The problem you are going to have is callback counting, using a library
// like async or step would make this party much much easier
that.minions = results; // bypass the db query after this
primaryCallback(null, results);
}
database.query('SELECT * FROM users WHERE owner = ' + data.id, aCallback);
};
var user = new User(someData);
user.getMinions(function(error, minions) {
if(error) {
throw error;
}
// Inside the function invoked by primaryCallback(...)
minions.forEach(function(minion) {
console.log('found this minion:', minion);
});
});
在这个例子中最值得注意的是回调. database.query(…)是异步的,你不想绑定等待它完成的事件循环.这是通过向查询提供回调aCallback来解决的,该回调在结果准备好时执行.一旦该回调触发,并且在对记录执行任何处理之后,您可以使用最终结果触发primaryCallback.
标签:javascript,mysql,node-js,timing,node-mysql 来源: https://codeday.me/bug/20190902/1788154.html