NodeJS:在循环中查询MS SQL将冻结响应 [英] NodeJS: Querying MS SQL in loop freezes response
问题描述
我正在使用访存来从NodeJS中的API构建读取数据.我有以下方法:
I am using fetch to read data from API build in NodeJS. I have the following method:
exports.read_detail = async function(req, res) {
try {
let pool = await new sql.ConnectionPool(config).connect();
pool.on('error', err => {
console.log("Pool Error");
console.log(err);
})
let resultDcn = await pool.request()
.input('DcnId', sql.Int, parseInt(req.body.DcnId))
.query('SELECT * FROM Dcn WHERE DcnId = @DcnId');
if(resultDcn.rowsAffected[0]) {
var DCNObj = resultDcn.recordset[0];
let resultDcnDay = await pool.request()
.input('DcnId', sql.Int, parseInt(DCNObj.DcnId))
.query('SELECT DcnDayId, DcnDate FROM DcnDay WHERE DcnId = @DcnId');
let timeClocks = [];
let myActivities = [];
for (var i = 0; i < resultDcnDay.recordset.length; i++) {
let dcnDayId = resultDcnDay.recordset[i].DcnDayId;
console.log("Day: " + dcnDayId);
/*Get Time Clocks per day*/
let resultDcnTimeClocks = await pool.request()
.input('DcnDayId', sql.Int, parseInt(dcnDayId))
.query('SELECT * FROM DcnDayTimeClock WHERE DcnDayId = @DcnDayId');
console.log(resultDcnTimeClocks.recordset);
timeClocks.push(resultDcnTimeClocks.recordset);
/*Get Activities per day*/
let resultDcnDayActivity = await pool.request()
.input('DcnDayId', sql.Int, parseInt(dcnDayId))
.query('SELECT DcnDayActivity.*, DcnDay.DcnDate FROM DcnDayActivity INNER JOIN DcnDay ON DcnDayActivity.DcnDayId = DcnDay.DcnDayId WHERE DcnDayActivity.DcnDayId = @DcnDayId');
console.log(resultDcnDayActivity.recordset);
myActivities.push(resultDcnDayActivity.recordset);
}
const DcnDto = {
"Dcn": DCNObj,
"timeClocks": timeClocks,
"dayActivities": myActivities
};
res.status(200).send({data: DcnDto});
} else {
res.status(200).send({msg: 'DCN does not exist'});
}
} catch(err){
console.log("Error: ", err);
res.status(400).send({msg: 'error ocurred: ' + err});
}
}
我猜测错误与返回的对象有关,因为如果我删除诸如dayActivities或Dcn之类的节点,则返回的响应可以. Dcn有12个属性.
I am guessing error has to do with the object returned, since if I remove a node like dayActivities or Dcn, response is returned ok. Dcn has like 12 attributes.
返回的对象是否有限制?或其他任何我应该知道的限制或格式?
Is there a limit on a object being returned? or any other limit or format I should know about?
推荐答案
我认为问题是myActivities
仅存在于if(resultDcn.rowsAffected[0])
的范围内.
I think the problem is that myActivities
only exists within the scope of the if(resultDcn.rowsAffected[0])
.
此时程序崩溃.
const DcnDto = {
"Dcn": DCNObj,
"timeClocks": timeClocks,
"dayActivities": myActivities
};
建议:当您使用异步/等待时,我建议您使用try catch.
Recommendation: When you use async/await I suggest using a try catch.
这篇关于NodeJS:在循环中查询MS SQL将冻结响应的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!