Syed Abdul Hannan
Syed Abdul Hannan

Reputation: 104

How to use a package in oneditprepare function in node template?

I am planning to make a select tag. It should show a list of files available in ftp server.

I am using basic-ftp npm package for this purpose and I am able to use it in the .js file but I am not sure how I have to use it in the oneditprepare function so that I can get the list of files available in the server.

My oneditprepare function is:

oneditprepare: async function () {

  example();
    ftpList = [];
    ftpListNames = [];

    async function example() {

      const ftp = require("basic-ftp");
      const client = new ftp.Client();
      client.ftp.verbose = true;
      try {
        await client.access({
          host: "192.168.104.105",
          user: "testuser",
          password: "1234",
        });
        // console.log(await client.list("/files"));
        ftpList = await client.list("/files");

        for (let i = 0; i < ftpList.length; i++) {
          ftpListNames.push(ftpList[i].name);
        }
        console.log("ftpListNames :>> ", ftpListNames);
      } catch (err) {
        console.log(err);
      }
      client.close();
    }
  }

But using this gives me an error saying require is not defined.

So I made a change to my settings.js file of node-red and added the package but still could not find a solution.

Upvotes: 0

Views: 298

Answers (1)

hardillb
hardillb

Reputation: 59866

Short answer, you don't

Longer answer:

You can not make FTP connections from the oneditprepare function because that runs in the browser. So you need to add all that code to the .js file and implement HTTP REST endpoints that can be from within the oneditprepare function to trigger it running in the Node-RED backend.

Upvotes: 1

Related Questions