我想将“结果”复制到“最终结果”,但是我需要填写缺失的日期。我在下面编写了代码,但无法解决问题。
router.get('/userstats/:id', async (req, res) => {
const { id } = req.params;
const conn = await connection(dbConfig).catch(e => {})
var results = await query(conn, 'SELECT * FROM historical_contract_prediction WHERE contractId = ?;', [id]).catch(console.log);
const error = {
"error": `This ${id} contractId does not exist! Are you lost check our documentation!`
}
var i = 0;
var last = [];
var finalResults = [];
var len = results.length;
x = 0;
while(x<len) {
last = results[x];
if(results[x+1] == undefined) {
break;
}
var start = new Date(results[x]["date"]);
var end = new Date(results[x+1]["date"]);
while(start<end) {
last["date"] = new Date(start);
finalResults[i++] = last;
var newDate = start.setDate(start.getDate() + 1);
start = new Date(newDate);
}
x++;
}
if(results == '') res.json(error);
res.set({ 'content-type': 'application/json; charset=utf-8' });
res.send( {finalResults} );
});
各种结果:
{
"contractId": 3573365,
"date": "2020-03-29T03:00:00.000Z",
"concludedWeight": 1,
"cancelledWeight": 0,
"calculatedStatus": "Concluded"
},
{
"contractId": 3573365,
"date": "2020-03-31T03:00:00.000Z",
"concludedWeight": 1,
"cancelledWeight": 0,
"calculatedStatus": "Concluded"
},
{
"contractId": 3573365,
"date": "2020-04-02T03:00:00.000Z",
"concludedWeight": 1,
"cancelledWeight": 0,
"calculatedStatus": "Concluded"
},
{
"contractId": 3573365,
"date": "2020-04-05T03:00:00.000Z",
"concludedWeight": 1,
"cancelledWeight": 0,
"calculatedStatus": "Concluded"
},
{
"contractId": 3573365,
"date": "2020-04-06T03:00:00.000Z",
"concludedWeight": 1,
"cancelledWeight": 0,
"calculatedStatus": "Concluded"
},
{
"contractId": 3573365,
"date": "2020-04-07T03:00:00.000Z",
"concludedWeight": 1,
"cancelledWeight": 0,
"calculatedStatus": "Concluded"
}
var finalResults:
{
"contractId": 3573365,
"date": "2020-03-29T03:00:00.000Z",
"concludedWeight": 1,
"cancelledWeight": 0,
"calculatedStatus": "Concluded"
},
{
"contractId": 3573365,
"date": "2020-03-31T03:00:00.000Z",
"concludedWeight": 1,
"cancelledWeight": 0,
"calculatedStatus": "Concluded"
},
{
"contractId": 3573365,
"date": "2020-03-31T03:00:00.000Z",
"concludedWeight": 1,
"cancelledWeight": 0,
"calculatedStatus": "Concluded"
},
{
"contractId": 3573365,
"date": "2020-04-02T03:00:00.000Z",
"concludedWeight": 1,
"cancelledWeight": 0,
"calculatedStatus": "Concluded"
},
{
"contractId": 3573365,
"date": "2020-04-02T03:00:00.000Z",
"concludedWeight": 1,
"cancelledWeight": 0,
"calculatedStatus": "Concluded"
},
{
"contractId": 3573365,
"date": "2020-04-05T03:00:00.000Z",
"concludedWeight": 1,
"cancelledWeight": 0,
"calculatedStatus": "Concluded"
},
{
"contractId": 3573365,
"date": "2020-04-05T03:00:00.000Z",
"concludedWeight": 1,
"cancelledWeight": 0,
"calculatedStatus": "Concluded"
},
{
"contractId": 3573365,
"date": "2020-04-05T03:00:00.000Z",
"concludedWeight": 1,
"cancelledWeight": 0,
"calculatedStatus": "Concluded"
},
{
"contractId": 3573365,
"date": "2020-04-06T03:00:00.000Z",
"concludedWeight": 1,
"cancelledWeight": 0,
"calculatedStatus": "Concluded"
}
当我在while循环中使用console.log时,finalResults变量为我提供了正确的结果。但是当发回邮件时,这些日期给了我错误的日期。
我对finalResults的期望:
{
"contractId": 3573365,
"date": "2020-03-29T03:00:00.000Z",
"concludedWeight": 1,
"cancelledWeight": 0,
"calculatedStatus": "Concluded"
},
{
"contractId": 3573365,
"date": "2020-03-30T03:00:00.000Z",
"concludedWeight": 1,
"cancelledWeight": 0,
"calculatedStatus": "Concluded"
},
{
"contractId": 3573365,
"date": "2020-03-31T03:00:00.000Z",
"concludedWeight": 1,
"cancelledWeight": 0,
"calculatedStatus": "Concluded"
},
{
"contractId": 3573365,
"date": "2020-04-01T03:00:00.000Z",
"concludedWeight": 1,
"cancelledWeight": 0,
"calculatedStatus": "Concluded"
},
{
"contractId": 3573365,
"date": "2020-04-02T03:00:00.000Z",
"concludedWeight": 1,
"cancelledWeight": 0,
"calculatedStatus": "Concluded"
},
{
"contractId": 3573365,
"date": "2020-04-03T03:00:00.000Z",
"concludedWeight": 1,
"cancelledWeight": 0,
"calculatedStatus": "Concluded"
},
{
"contractId": 3573365,
"date": "2020-04-04T03:00:00.000Z",
"concludedWeight": 1,
"cancelledWeight": 0,
"calculatedStatus": "Concluded"
},
{
"contractId": 3573365,
"date": "2020-04-05T03:00:00.000Z",
"concludedWeight": 1,
"cancelledWeight": 0,
"calculatedStatus": "Concluded"
},
{
"contractId": 3573365,
"date": "2020-04-06T03:00:00.000Z",
"concludedWeight": 1,
"cancelledWeight": 0,
"calculatedStatus": "Concluded"
}
答案 0 :(得分:0)
尝试实现异步等待。我认为错误的部分是:
while(start<end) {
last["date"] = new Date(start);
finalResults[i++] = last;
var newDate = start.setDate(start.getDate() + 1);
start = new Date(newDate);
}
x++;
仅当开始小于结束时才执行。但是您的console.log会在循环之前打印结果。