gpt4 book ai didi

node.js - 在循环迭代期间不会重新分配值。这是我的第一个 Node 项目,我想我可能会陷入异步 js 概念

转载 作者:太空宇宙 更新时间:2023-11-04 01:27:10 25 4
gpt4 key购买 nike

我在 get 请求的第一个循环中分配了变量(如 requestNo)。每次迭代后不会重新分配变量。我正在跟踪每个 pdf 进程的 promise 状态,并在循环之后等待 promise 在归档所有 PDF 之前得到解决。

app.get('/VisaApplications.zip/:amount*?', function(req, res) {
debugger;
let amount = req.params.amount;
let archive = archiver('zip');
let promises = [];
archive.pipe(res);
archive.on('error', function(err) {
throw err;
});

if (!amount) {
amount = 1;
}

for ( let i = 0; i < amount; i++ ) {
let date = new Date();
let datetime = date.getTime();
let seconds = datetime / 1000;
let timestamp = Math.floor(seconds);
let checksum = 103;
let sNumber = timestamp.toString();
let randInt = Math.floor(Math.random() * 9999) + 1;
let requestNo = randInt + sNumber

for (let j = 0, len = requestNo.length; j < len; j += 1) {
checksum += code128.indexOf(requestNo.charAt(j)) * j + 1;
}

checksum %= 103;
let barcode1 = code128[103] + requestNo + code128[checksum] + code128[106];
let barcode2 = '* ' + requestNo + ' *';
promises.push( pdftk
.input(pdfFilePath)
.fillForm({
request_no: requestNo,
Barcode1: barcode1,
Barcode2: barcode2,
})
.output()
.then(buffer => {
filename = 'VisaApplication' + (i + 1) + '.pdf';
archive.append(buffer, { name: filename });
})
.catch(err => {
throw err;
})
);
}
res.writeHead(200, {
'Content-Type': 'application/pdf',
'Content-Disposition': 'attachment; filename=VisaApplications.zip'
});
Q.allSettled(promises).done(function() {
archive.finalize();
});
});

编辑在循环外部添加了一个数据存储并传递给pdf处理函数不起作用。所以,这不是循环的时间问题......我认为。

app.get('/VisaApplications.zip/:amount*?', function(req, res) {
debugger;
let amount = req.params.amount;
let archive = archiver('zip');
let promises = [];
let pdfFormData = {};
archive.pipe(res);
archive.on('error', function(err) {
res.sendStatus(500);
});

if (!amount) {
amount = 1;
}

for ( let i = 0; i < amount; i++ ) {
let date = new Date();
let datetime = date.getTime();
let seconds = datetime / 1000;
let timestamp = Math.floor(seconds);
let checksum = 103;
let sNumber = timestamp.toString();
let randInt = Math.floor(Math.random() * 9999) + 1;
let requestNo = randInt + sNumber;

for (let j = 0, len = requestNo.length; j < len; j += 1) {
checksum += code128.indexOf(requestNo.charAt(j)) * j + 1;
}

checksum %= 103;
let barcode1 = code128[103] + requestNo + code128[checksum] + code128[106];
let barcode2 = '* ' + requestNo + ' *';

pdfFormData[i] = {
request_no: requestNo,
Barcode1: barcode1,
Barcode2: barcode2
};

promises.push( pdftk
.input(pdfFilePath)
.fillForm(pdfFormData[i])
.output()
.then(buffer => {
filename = 'VisaApplication' + (i + 1) + '.pdf';
archive.append(buffer, { name: filename });
})
.catch(err => {
res.sendStatus(500);
})
);
}
res.writeHead(200, {
'Content-Type': 'application/pdf',
'Content-Disposition': 'attachment; filename=VisaApplications.zip'
});
Q.allSettled(promises).done(function() {
archive.finalize();
});
});

我希望在每次循环迭代期间重新分配变量,以便将不同的值添加到 pdf 文件中。

最佳答案

因为我的评论似乎可以帮助您了解正在发生的事情,所以写下答案。

let 每次通过 for 循环都会创建一个新变量。

您是否意识到 for 循环运行得非常快(不等待任何异步操作完成),因此 datetime 很可能对于所有迭代都有几乎相同的值你的循环。并且,timestampsNumber 均源自datetime。所以。只有 randInt 从循环的一次迭代到下一次迭代会有很大变化。

看来您也在 .append() 之前执行 .pipe() ,因此可读存档可能会到达末尾并在完成之前关闭其流.append() 操作。

要解决这个问题,您可以在 .append() 操作之后执行 .pipe(),也可以更改流,这样它们就不会自动当它们到达末尾时关闭(我不确定当您 .append() 到它们时它们是否会自动重新启动,但您必须尝试)。然后,如果您已关闭自动关闭功能,则必须在完成后手动关闭流。

关于node.js - 在循环迭代期间不会重新分配值。这是我的第一个 Node 项目,我想我可能会陷入异步 js 概念,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57154730/

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