[英]array value is not updating in for loop in nodejs
I am trying to push data in an array after downloading the image from download function. 从下载功能下载图像后,我试图将数据推送到数组中。 It's a problem in nodejs promise.
这是nodejs承诺中的问题。 How can I fix this issue.
我该如何解决此问题。
current output: 当前输出:
[
{
sku: 99104942591,
retailer: 'JCREWFCT',
images: []
}
]
expected output: 预期输出:
[
{
sku: 99103497136,
retailer: 'JCREWFCT',
images: [
"http://localhost:4001/JCREWFCT/99103497136.png",
"http://localhost:4001/JCREWFCT/99103497136_1.png"
]
}
]
in outputArr
I am trying to store data 在
outputArr
我试图存储数据
var downloadImages = async function() {
var outputArr = [];
for(var i=0; i<excelJsonArr.length; i++) {
var d = excelJsonArr[i];
var out = {
sku : d.sku,
retailer : d.retailer,
images : []
}
if(d.image_link_1) {
var saveDir = path.join('./public', d.retailer, d.sku+'.png');
var imgUrl = properties.get('protocol') + '://' + properties.get('hostname') + ':' + properties.get('port') + '/' + d.retailer + '/' + d.sku + '.png';
await download(d.image_link_1, saveDir, function(){
out.images.push(imgUrl);
});
}
if(d.image_link_2) {
var saveDir = path.join('./public', d.retailer, d.sku+'_2.png');
await download(d.image_link_1, saveDir, function(){
var imgUrl = properties.get('protocol') + '://' + properties.get('hostname') + ':' + properties.get('port') + '/' + d.retailer + '/' + d.sku + '_2.png';
out.images.push(imgUrl);
});
}
outputArr.push(out);
}
console.log(outputArr);
}
var download = async function(uri, filename, callback){
await request.head(uri, function(err, res, body){
console.log('content-type:', res.headers['content-type']);
console.log('content-length:', res.headers['content-length']);
request(uri).pipe(fs.createWriteStream(filename)).on('close', callback);
});
};
I don't know what your download-function actually does but normally, when working with asnyc, you would do this: 我不知道您的下载功能实际上在做什么,但是通常,在使用asnyc时,您可以这样做:
await download(d.image_link_1, saveDir);
out.images.push(imgUrl);
and I you should try to work with try
to catch
any errors coming from download, see: Correct Try…Catch Syntax Using Async/Await 我和你应该尝试与合作
try
,以catch
从下载来的任何错误,请参见: 正确的try ... catch语法使用异步/等待
FYI, next time, share more of your code and if possible a reproduceable code or example GitHub repo, so we can see the error by ourself. 仅供参考,下次,您可以共享更多代码,并在可能的情况下共享可重现的代码或示例GitHub存储库,因此我们可以亲自看到错误。
Since you are using async
await
you don't need to use callback function. 由于您正在使用
async
await
您无需使用回调函数。 Just call the desired functions after await
await
后只需调用所需的功能
var downloadImages = async function () {
var outputArr = [];
for (var i = 0; i < excelJsonArr.length; i++) {
var d = excelJsonArr[i];
var out = {
sku: d.sku,
retailer: d.retailer,
images: []
}
if (d.image_link_1) {
var saveDir = path.join('./public', d.retailer, d.sku + '.png');
var imgUrl = properties.get('protocol') + '://' + properties.get('hostname') + ':' + properties
.get('port') + '/' + d.retailer + '/' + d.sku + '.png';
await download(d.image_link_1, saveDir, function () {
// out.images.push(imgUrl);// <-- not here
});
out.images.push(imgUrl); // <-- here
}
if (d.image_link_2) {
var saveDir = path.join('./public', d.retailer, d.sku + '_2.png');
await download(d.image_link_1, saveDir, function () {
/* var imgUrl = properties.get('protocol') + '://' + properties.get('hostname') + ':' +
properties.get('port') + '/' + d.retailer + '/' + d.sku + '_2.png';
out.images.push(imgUrl); */ // <-- not here
});
var imgUrl = properties.get('protocol') + '://' + properties.get('hostname') + ':' +
properties.get('port') + '/' + d.retailer + '/' + d.sku + '_2.png';
out.images.push(imgUrl); // <-- here
}
outputArr.push(out);
}
console.log(outputArr);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.