Nodejs save link to variable

I upload a file into OpenKM and get the URI to the file as response. I want to save the link as processVariable.

var url = "http://172.20.16.90:8080/OpenKM/frontend/index.jsp?uuid=" + JSON.parse(res.uuid);
var variables = new Variables();
variables.set("link", url);

The variable is created but the link is empty. I tried encodeURIComponent but no success. What should i do?

Thank you!

Hi @mbrain,

could you add more context? Where did you put the lines you posted? Do you access the variables later in your code?

Cheers, Ingo

Sorry, this is the full script

const { Client, logger } = require("camunda-external-task-client-js");
const { Variables } = require("camunda-external-task-client-js");
const { exec } = require('child_process');
const mysql = require('mysql');
const http = require('http');
const fs = require('fs');
  
const config = { baseUrl: "http://localhost:8080/engine-rest" };
const client = new Client(config);


var con = mysql.createConnection({
  host: "localhost",
  user: "root",
  database: "demo",
  password: "demo"
});

var moment = require("moment");

client.subscribe("Rechnung", async function({ task, taskService }) {

  console.log("*** Processing task " + task.id);
  var uploadedFile = task.variables.get("rechnung");

  const file = fs.createWriteStream(uploadedFile.filename);
  const request = http.get("http://localhost:8080/engine-rest" + uploadedFile.remotePath, function(response) {

        response.pipe(file);
        file.on("finish", () => {
                file.close();
        });
  });


  var cmd = "curl -u okmAdmin:admin -H \"Accept: application/json\" -X POST -F docPath=\"/okm:root/manuel/" + task.id + "_" + uploadedFile.filename + "\" -F content=\"@" + uploadedFile.filename + "\" http://172.20.16.90:8080/OpenKM/services/rest/document/createSimple";

  /*
  Here i create the variable for the link
  */
  var newlink = "";
  
  exec(cmd, (err, stdout, stderr) => {

        console.log("*** Link generated: http://172.20.16.90:8080/OpenKM/frontend/index.jsp?uuid=" + JSON.parse(stdout).uuid);
        
        newlink = "http://172.20.16.90:8080/OpenKM/frontend/index.jsp?uuid=" + JSON.parse(stdout).uuid;

        if(err)
            console.log("error " + JSON.stringify(stderr));

        // store to database
        else {
			
            con.connect(function(err) {
                var start = moment().utc().format("YYYY-MM-DD HH:mm:ss");
                con.query("INSERT INTO statistics.tasks (id, time, taskId) VALUES (null, \"" + start + "\", \"" + task.id + "\");", function (err, result) { });
            });
			
        }

  });

  var processVariables = new Variables();

  processVariables.set("link", newlink );

  await taskService.complete(task, processVariables);

});

Thank you :smiley:

I found the problem (not the solution), its because the async function does not wait for exec to finish so it finishes the task before the output was received and therefore the variable is empty. Now i just have to figure out how to solve this.

Now it works.

const { Client, logger } = require("camunda-external-task-client-js");
const { Variables } = require("camunda-external-task-client-js");
const { exec } = require('child_process');

const http = require('http');
const fs = require('fs');

const config = { baseUrl: "http://localhost:8080/engine-rest" };
const client = new Client(config);

// rechnungseingang
client.subscribe("Rechnung", async function({ task, taskService }) {

  console.log("*** Processing task " + task.id);

  // get the uploaded file
  var uploadedFile = task.variables.get("rechnung");

  // save it to disk
  const file = fs.createWriteStream(uploadedFile.filename);
  const request = http.get("http://localhost:8080/engine-rest" + uploadedFile.remotePath, function(response) { response.pipe(file); file.on("finish", () => { file.close(); }); });

  // upload to openkm
  var cmd = "curl -u okmAdmin:admin -H \"Accept: application/json\" -X POST -F docPath=\"/okm:root/ibass/" + task.id + "_" + uploadedFile.filename + "\" -F content=\"@" + uploadedFile.filename + "\" http://192.168.0.31:8080/OpenKM/services/rest/document/createSimple";

  let newlink = await sh(cmd);

  var processVariables = new Variables();

  console.log("*** newlink = " + newlink);

  processVariables.set("link", newlink);

  await taskService.complete(task, processVariables);

});

function sh(cmd) {
    return new Promise(resolve => {

        exec(cmd, (err, stdout, stderr) => {

            let link;
            link = JSON.parse(stdout).uuid;
            if(err)
                console.log("error " + JSON.stringify(stderr));
            resolve(link);

        });
    })
}

Hi @mbrain,

thank you for sharing your solution here. These kind of async JavaScript programming is beyond my comfortzone…

Cheers, Ingo

1 Like