gpt4 book ai didi

mysql - Expressjs : Open view after foreach with mysql result.

转载 作者:行者123 更新时间:2023-11-29 21:42:56 26 4
gpt4 key购买 nike

我正在尝试使用 mysql 查询的结果进行循环,并在该循环中执行一个新查询。循环遍历结果后,我想打开一个包含第一个和第二个查询结果的新 View 。

现在我的问题:循环工作正常,我可以打开新 View ,但是打开 View 时我只得到第一个查询的结果和第二个查询的一部分,它在运行第二个查询之前打开 View 时间。

我的代码:

 function getActivitiesDate(req,res, rows)
{
var rows2 = [];
rows.forEach(function getoutput(item) {
console.log("getActivitiesDate openend");
connectionpool.getConnection(function(err, connection3) {
console.log('Trying to connect to activities date');
if (!err) {
console.log('Trying to execute query for activities now'),
connectionpool.query('SELECT * FROM CALENDAR_ACTIVITY WHERE ACTIVITY_ID =' + connection3.escape(item.ACTIVITY_ID),
function (err, rows2) {
if (err) {
console.error(err);
res.statusCode = 500;
res.send({
result: 'error',
err: err.code
});
connection3.release();
console.log('It doesnt work error 500');
}
if (rows2.length == 0) {
res.render('index', {title: 'Index - Calendar Activity not found'});
}
console.log('Show activities + dates');



// connection3.release();
});
} else {
console.error('CONNECTION error: ', err);
res.statusCode = 503;
res.send({
result: 'error',
err: err.code
});
console.log('It does not work...');
}
});

});
console.log('This is the result of rows2Result: ');
console.log(rows2);
res.render('viewUserActivities', {activities:rows, activityDates:rows2 , title: 'Created activities'});

}

“rows”属性目前包含 2 行(这可能更多或更少,这就是我有“rows.forEach”的原因),但我不确定如何将“rows2”的结果传递给在每个结果都通过 foreach 循环循环之后的新 View 。

任何帮助将不胜感激。我不清楚,或者您需要更多信息,请随时询问。

最佳答案

我没有足够的代表来发表评论,因此我可能会误解这个问题,但是您可以在 forEach 回调完成后渲染 View 吗?

像这样:

function getActivitiesDate(req,res, rows)
{
var rows2 = [];
rows.forEach(function getoutput(item) {
console.log("getActivitiesDate openend");
connectionpool.getConnection(function(err, connection3) {
console.log('Trying to connect to activities date');
if (!err) {
console.log('Trying to execute query for activities now'),
connectionpool.query('SELECT * FROM CALENDAR_ACTIVITY WHERE ACTIVITY_ID =' + connection3.escape(item.ACTIVITY_ID),
function (err, rows2) {
if (err) {
console.error(err);
res.statusCode = 500;
res.send({
result: 'error',
err: err.code
});
connection3.release();
console.log('It doesnt work error 500');
}
else if (rows2.length == 0) {
res.render('index', {title: 'Index - Calendar Activity not found'});
} else {
console.log('This is the result of rows2Result: ');
console.log(rows2);
res.render('viewUserActivities', {activities:rows, activityDates:rows2 , title: 'Created activities'});
console.log('Show activities + dates');

}

// connection3.release();
});
} else {
console.error('CONNECTION error: ', err);
res.statusCode = 503;
res.send({
result: 'error',
err: err.code
});
console.log('It does not work...');
}
});

关于mysql - Expressjs : Open view after foreach with mysql result.,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34344753/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com