Problem running a code - Please return an array of objects, one for each item you would like to output

Describe the problem/error/question

Running this code I have an error, but the code runs in VSCode

const sftpClient = require(‘ssh2-sftp-client’);
const fs = require(‘fs’);

const sftp = new sftpClient();
sftp.connect({
host: ‘sftp-slc.cmsw.com’,
port: ‘22’,
username: ‘H_36266751’,
password: ‘’,
privateKey: fs.readFileSync(‘/Users/cassio/bosco/H_36266751.pem’)
}).then(() => {
return sftp.cwd();

}) .then(() => {
return sftp.list(‘/retorno’);
}).then(async data => {
for( var i = 0, len = data.length; i < len; i++) {
console.log(data[i].name);
const remoteFile = ‘/retorno/’ + data[i].name
const localFile = ‘/Users/cassio/bosco/’+ data[i].name
try { await sftp.fastGet(remoteFile, localFile) }
catch(err) {
console.log('GET ERROR = '+ err)
};
}
}).then(() => {
sftp.end();
}).catch(err => {
console.log(Error: ${err.message});
});

What is the error message (if any)?

ERROR: Code doesn’t return items properly

Please return an array of objects, one for each item you would like to output

Error: Code doesn't return items properly
    at JavaScriptSandbox.validateRunCodeAllItems (/usr/local/lib/node_modules/n8n/node_modules/n8n-nodes-base/nodes/Code/Sandbox.ts:88:10)
    at JavaScriptSandbox.runCodeAllItems (/usr/local/lib/node_modules/n8n/node_modules/n8n-nodes-base/nodes/Code/JavaScriptSandbox.ts:70:15)
    at Object.execute (/usr/local/lib/node_modules/n8n/node_modules/n8n-nodes-base/nodes/Code/Code.node.ts:135:13)
    at Workflow.runNode (/usr/local/lib/node_modules/n8n/node_modules/n8n-workflow/src/Workflow.ts:1307:8)
    at /usr/local/lib/node_modules/n8n/node_modules/n8n-core/src/WorkflowExecute.ts:1045:29

Please share your workflow

(Select the nodes on your canvas and use the keyboard shortcuts CMD+C/CTRL+C and CMD+V/CTRL+V to copy and paste the workflow.)

Share the output returned by the last node

Information on your n8n setup

  • n8n version:
  • Database (default: SQLite):
  • n8n EXECUTIONS_PROCESS setting (default: own, main):
  • Running n8n via (Docker, npm, n8n cloud, desktop app):
  • Operating system:

It looks like your topic is missing some important information. Could you provide the following if applicable.

  • n8n version:
  • Database (default: SQLite):
  • n8n EXECUTIONS_PROCESS setting (default: own, main):
  • Running n8n via (Docker, npm, n8n cloud, desktop app):
  • Operating system:

n8n version : 1.11.2
Database : none
n8n executions process : i don`t know
running n8n via npm
operating system : mac os

Hey,
You need to return the result as an array of objects.
Try something like this:

const sftpClient = require('ssh2-sftp-client');
const fs = require('fs');

const sftp = new sftpClient();
sftp.connect({
    host: 'sftp-slc.cmsw.com',
    port: '22',
    username: 'H_36266751',
    password: '',
    privateKey: fs.readFileSync('/Users/cassio/bosco/H_36266751.pem')
}).then(() => {
    return sftp.cwd();
})
.then(() => {
    return sftp.list('/retorno');
})
.then(async data => {
    let results = [];
    for (var i = 0, len = data.length; i < len; i++) {
        const remoteFile = '/retorno/' + data[i].name;
        const localFile = '/Users/cassio/bosco/' + data[i].name;
        try {
            await sftp.fastGet(remoteFile, localFile);
            results.push({file: data[i].name, status: 'Success'});
        } catch (err) {
            console.log('GET ERROR = ' + err);
            results.push({file: data[i].name, status: 'Failed', error: err.message});
        }
    }
    return results;
})
.then(results => {
    sftp.end();
    // Return results to n8n
    return results;
})
.catch(err => {
    console.log('Error: ' + err.message);
});

Hi CristianG

Unfortunately, worked fine in VSCode, but not in n8n

I believe there is something different about how n8n runs JavaScript

Rgds

Hey @cassiodamasceno,

There is a bit of a difference because of the sandbox but we do also expect a specific return format which needs to be an array of objects.

Understood Jon

Could you help on how to change the code to acomplish this ?

Rgds

Hey @cassiodamasceno,

Does the example provided by @CristianG not do it?

No Jon

I receive the same error

Hey @cassiodamasceno,

What is the node trying to return? It could just be that it wants a return at the end as well so maybe a return $input.all() at the end will sort it.

Jon, in fact, the only thing that this code does, is download files from a remote Sftp server and save them on a local directory

Hey @cassiodamasceno,

It still needs to return an array once complete as that is what we expect.

Understood

I believe the problem is that the code has many intermediate returns and not once complete
I’ll try to figure out how to resolve it