- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我在 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
很可能对于所有迭代都有几乎相同的值你的循环。并且,timestamp
和sNumber
均源自datetime
。所以。只有 randInt
从循环的一次迭代到下一次迭代会有很大变化。
看来您也在 .append()
之前执行 .pipe()
,因此可读存档可能会到达末尾并在完成之前关闭其流.append()
操作。
要解决这个问题,您可以在 .append()
操作之后执行 .pipe()
,也可以更改流,这样它们就不会自动当它们到达末尾时关闭(我不确定当您 .append()
到它们时它们是否会自动重新启动,但您必须尝试)。然后,如果您已关闭自动关闭功能,则必须在完成后手动关闭流。
关于node.js - 在循环迭代期间不会重新分配值。这是我的第一个 Node 项目,我想我可能会陷入异步 js 概念,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57154730/
使用登录后,我想吐出用户名。 但是,当我尝试单击登录按钮时, 它给了我力量。 我看着logcat,但是什么也没显示。 这种编码是在说。 它将根据我在登录屏幕中输入的名称来烘烤用户名。 不会有任何密码。
关闭。这个问题不符合Stack Overflow guidelines .它目前不接受答案。 这个问题似乎是题外话,因为它缺乏足够的信息来诊断问题。 更详细地描述您的问题或include a min
我是一名优秀的程序员,十分优秀!