Could not do iterative Get in a loop
See original GitHub issueI did a List to get all the files in a remote path. After which, I tried to loop through each of the file in the List and did a Get. The Get did not happened. I just started the node.js journey. Any advice is greatly appreciated. Apologies that the indentation in the code does not seems to work when I post. Thanks.
Here’s the code:
let Client = require('ssh2-sftp-client');
let client = new Client();
const config = {
host: 'XXXX.XXX.com.sg',
port: '1022',
username: 'XXXX',
password: 'XXXXX',
retries: 3
};
let remotePath = '/OUT/SPH/PROCESSED/';
let localPath = '/tmp/';
client.connect(config)
.then(() => {
return client.list(remotePath,"ifss*");
})
.then((listing) => {
listing.forEach(item => {
let remoteFile = remotePath + item.name;
let localFile = localPath + item.name;
console.log(remoteFile + " , " + localFile);
client.get(remoteFile, localFile)
.then((file) => {
console.log(file + " download successful.");
})
.catch((err) => {
console.log(err);
});
});
return true;
})
.then(() => {
console.log("File downloaded");
return client.end();
})
.catch(err => {
console.error(err.message);
return(err.message);
})
Here’s the output:
ngky@instance-1:$ node ssh2-sftp-client-get4.js
/OUT/SPH/PROCESSED/ifss_0309.txt03-09-2019-12:45 , /tmp/ifss_0309.txt03-09-2019-12:45
/OUT/SPH/PROCESSED/ifss_1109.txt11-09-2019-11:30 , /tmp/ifss_1109.txt11-09-2019-11:30
/OUT/SPH/PROCESSED/ifss_1209.txt12-09-2019-11:30 , /tmp/ifss_1209.txt12-09-2019-11:30
/OUT/SPH/PROCESSED/ifss_1309.txt13-09-2019-11:30 , /tmp/ifss_1309.txt13-09-2019-11:30
/OUT/SPH/PROCESSED/ifss_1409.txt14-09-2019-11:30 , /tmp/ifss_1409.txt14-09-2019-11:30
/OUT/SPH/PROCESSED/ifss_1509.txt15-09-2019-11:30 , /tmp/ifss_1509.txt15-09-2019-11:30
/OUT/SPH/PROCESSED/ifss_2308.txt23-08-2019-17:43 , /tmp/ifss_2308.txt23-08-2019-17:43
/OUT/SPH/PROCESSED/ifss_2308.txt25-08-2019-16:15 , /tmp/ifss_2308.txt25-08-2019-16:15
File downloaded
ngky@instance-1:$ ls -l /tmp
total 8
drwx------ 2 ngky ngky 4096 Sep 17 00:10 ssh-dNrB92wH00
drwx------ 2 ngky ngky 4096 Sep 17 00:10 ssh-Xw61UHqyHz
Issue Analytics
- State:
- Created 4 years ago
- Comments:6 (3 by maintainers)
Top Results From Across the Web
Python Loop Iteration issue - Stack Overflow
You're iterating over a_list by value, but you then attempt to use the loop variable i to iterate by index. Don't do this....
Read more >Why is this code not working ? Why does It stops looping
For a loop iteration to run it ONLY tests for the existence of a letter at the index that matches the internal loop...
Read more >Fundamentals of Loop and Iteration in Programming
What is iteration in programming? Iteration is executing a sequence of code instructions specified times or until a specific condition is true.
Read more >How do I force the next loop iteration if error occurs within the ...
I have a loop that is supposed to run a very long time that starts with a webread command. Sometimes there is something...
Read more >for...of - JavaScript | MDN - MDN Web Docs
Note: Each iteration creates a new variable. Reassigning the variable inside the loop body does not affect the original value in the ...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Your not quite got your understanding of promises down yet. You might find this link useful - You Don’t Know JS. Kyle’s books are good and his book on async and promises will help a lot. You need to understand the asynchronous nature of promises.
While you can do what yiour trying to do with basic Promises, it gets a bit tredious. You ahve to structure your code so that it is a long Promise.then.then.then… chain. A better approach is to use async, which is a way to use promises, but in a more synchronous manner that avoids the nested .then chain. For example (untested) -
As your learning, make sure you use a good editor like Visual Studio Code which will provide lots of help, especially if you add extensions like eslint and prettier etc.
P.S. To format your code in future posts, use the github markup.
Is working based on 1st example! Thanks Tim.