tina
tina

Reputation: 35

nodejs download multiple files from web

I am trying to download multiple files from a OneDrive folder. Below has my code but it will only download the last file and not all of them

for(const f in files)
{
    var fileURL = (files[f]["@microsoft.graph.downloadUrl"]);
    var fileName = (JSON.stringify(files[f].name)).slice(1,-1);

    var request = https.get(fileURL, function(response){
        console.log(fileURL);
        if(response.statusCode == 200){
            var file = fs.createWriteStream(`./temp/${userId}/${fileName}`);
            response.pipe(file);
        }
        request.setTimeout(60000,function(){
            request.destroy();
        });
    });
}

i.e the console log would print

FILE_URL1
FILE_URL1
FILE_URL1

rather than

FILE_URL1
FILE_URL2
FILE_URL3

Note that if the console.log(fileURL) is placed before var request https.get... it prints out the 3 file urls. I'm not sure if its a problem with the loops or if there is something else. I am quite new at javascript so I dont know a lot.

Upvotes: 0

Views: 1049

Answers (1)

Replace the var with const or let you will see the different result
Description: What's the difference between using "let" and "var"?

Upvotes: 1

Related Questions