我有mysql查询的序列。第一个查询结果用于另一个查询,依此类推。因此,我想按顺序执行查询。代码是用不同的方法分开的。但由于异步行为,这是同时工作的。
我试着用 dountil
, async.forever
, waterfall
以及 series
没有mysql,一切都很好。但是当我添加mysql代码时,由于查询的异步行为,它不能正常工作。这是我的密码:
app.post('/insert', async function(req, res, next)
{
var jsondata = req.body;
var flag = 0;
var offset = 0;
async.whilst(
function test()
{
return flag == 0;
},
function chk(callback)
{
connection.query('SELECT nlc,id,user_id,coin_id FROM tbl_buy WHERE `type`="buy" AND `status`="available" AND coin_id="'+jsondata.coinid+'" AND order_type="market" order by id ASC LIMIT '+offset+',1',function (error,results, fields)
{
if(results.length > 0)
{
connection.query('SELECT tbl_buy.* from (select tbl_buy.*, (@sum := @sum + nlc) as cume_stock from tbl_buy cross join (select @sum := 0) params WHERE tbl_buy.type="sell" AND tbl_buy.status="available" AND tbl_buy.user_id!="'+results[0].user_id+'" AND tbl_buy.coin_id="'+jsondata.coinid+'" AND tbl_buy.order_type="market" order by id ) tbl_buy where cume_stock-nlc < "'+results[0].nlc+'"', async function (error,results_arr, fields)
{
if(results_arr.length > 0)
{
flag = 1;
// here both methods execute simultaneously instead of one after another
await executeOrder(results,results_arr); // this method contains mysql queries
await executeLimitOrders(jsondata.coinid); // this method contains mysql queries
callback();
}
else
{
offset++;
callback()
}
});
}
});
},
function callback(err) {
if(err)
console.log(err);
console.log('Job completed..!!');
//executeLimitOrders(jsondata.coinid);
}
);
})
请给出一个正确的方法来按顺序执行mysql查询。在执行完所有函数之后,我想再次调用另一个包含mysql代码的方法。
暂无答案!
目前还没有任何答案,快来回答吧!