question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Could not do iterative Get in a loop

See original GitHub issue

I 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:closed
  • Created 4 years ago
  • Comments:6 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
theophilusxcommented, Sep 17, 2019

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) -

"use strict";

const Client = require('ssh2-sftp-client');

const client = new Client();

const config = {
  host: 'XXXX.XXX.com.sg',
  port: '1022',
  username: 'XXXX',
  password: 'XXXXX',
  retries: 3
};

async function downloadAll(remotePath, localPath) {
  await client.connect(config);
  let listings = await client.list(remotePath, "ifss*");
  listings.forEach(async item => {
    let remoteFile = remotePath + item.name;
    let localFile = localPath + item.name;
    console.log(`Remote: ${remoteFile} Local: ${localFile}`);
    let res = await client.get(remoteFile, localFile);
    console.log(`${res} downloaded`);
  });
  await client.end();
}

downloadAll('/OUT/SPH/PROCESSED/', '/tmp/')
  .then(() => {
    console.log("all files downloaded");
  })
  .catch(err => {
    console.log(`An error occured: ${err.message}`);
  });

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.

0reactions
ngkycommented, Sep 18, 2019

Is working based on 1st example! Thanks Tim.

"use strict";

const Client = require("ssh2-sftp-client");

const client = new Client();

const config = {
  host: 'demo.wftpserver.com',
  port: '2222',
  username: 'demo-user',
  password: 'demo-user',
  retries: 3
};

async function downloadAll(remotePath, localPath) {
  await client.connect(config);
  let listings = await client.list(remotePath, "wftpserver*");
  
  for (let item of listings) {
    let remoteFile = remotePath + item.name;
    let localFile = localPath + item.name;
    console.log(`Remote: ${remoteFile} Local: ${localFile}`);
    let res = await client.get(remoteFile, localFile);
    console.log(`${res} downloaded`);
  }
  await client.end();
}

downloadAll("/download/", "/tmp/")
  .then(() => {
    console.log("all files downloaded");
  })
  .catch(err => {
    console.log(`An error occured: ${err.message}`);
  });

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found