Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.



Info

In PJS 6.0, you can debug low-code using the Designer IDE. This tip documents a different way to debug a low-code module, by "converting" it to "real" PJS code, so that you can see all the details in the PJS code generated from the low-code JSON specs. It's also a good way to learn "when I use a plugin, what does that plugin do behind the scenes, what actual 'real' PJS code does it run?".


Example of a low-code module with a bug

  • Navigate to the workspace "pjstips".
  • Click New/Module File.
  • Click Edit/Source, to bring up the Source view.

  • Clear the current default code, and enter the code below.

  • Code Block
    languagejs
    title pjstips_04_1.module.json
    {
      "routines": [
        {
          "name": "routine1",
          "inputs": [
            {
              "type": "decimal",
              "ibmiLength": 4,
              "name": "custno",
              "ibmiDecimals": 0
            }
          ],
          "outputs": [
            {
              "type": "string",
              "ibmiLength": 30,
              "name": "name"
            }
          ],
          "steps": [
            {
              "text": "Consume REST service",
              "answers": {
                "plugin": "Custom:web-service",
                "uri": "http://localhost:8081/run/pjstips/pjstips_02_3",
                "method": "POST",
                "headers": "{\"Content-Type\": \"application/x-www-form-urlencoded\"}",
                "body": "{\n  \"custno\": input[\"custno\"]\n}",
                "json": true,
                "auth": "",
                "capture_response": true,
                "capture_on_error": false,
                "capture_as": "Value",
                "specific_property": "",
                "destination": "Work variable",
                "work_variable": "myres"
              }
            },
            {
              "text": "Set Module output",
              "answers": {
                "plugin": "Program Data:set-module-output",
                "module-output-values": {
                  "name": "myres[\"name\"]"
                }
              }
            }
          ]
        }
      ]
    }


  • Click Edit/Design, to bring up the Design view.
  • If needed, in the "Consume REST service" step, change the Endpoint URL to point to the correct host/port.
  • Click Home/Save As, and save as pjstips_04_1.module.json.
  • You've created a low-code module, as shown below, with a routine named "routine1" that:
    • Takes an input parameter of "custno".
    • Uses plug "Consume REST service" to issue a POST request to URL http://localhost:8081/run/pjstips/pjstips_02_3 (created in a previous example).
    • The input "custno" is specified in the "body" of the POST request.
    • The returned JSON object from the REST web service is saved in a work variable named "myres".
    • The value of myres["name"] is used to set the output value output["name"].
    • But, it has a bug; we'll learn how to debug this problem.

...

  • 5. Copy/paste that code to somewhere (e.g. Notepad++) and save it.
  • 6. Close the file. Do NOT save it! If you save it, your module now will contain real code, not low-code anymore.
  • 7. Now, create a new web service with the code saved in step (5) above, with some minor adjustments, so that you can call it from a browser and debug it. As a general reference, click here to see how to create a web service. Follow the steps below to create a web service for this example.
  • 8. Click New/Javascript file.
  • 9. Enter the code below. This is pretty much the same as the code you saved in step (5) above, but changed so that it can be called as a web service from the browser, so you can debug it easily.
Code Block
languagejs
titlepjstips_04_2.js
function app(req, res) {
  
  // init these 2 objects that would normally be done when a low-code module is called
  var input = {};
  var output = {};

  // set input data
  input.custno = 1234;

  // start of code converted from low-code module in step (5) above --------------------------
  // Consume REST service
  var _data = pjs.sendRequesthttpRequest({
    method: "POST",
    uri: `http://localhost:8081/run/pjstips/pjstips_02_3`,
    headers: {"Content-Type": "application/x-www-form-urlencoded"},
    body: {
    "custno": input["custno"]
    },
    json: true
  });
  var myres = _data;

  // Set Module output
  output["name"] = myres["name"];
  // end of code converted from low-code module in step (5) above ----------------------------
  
  // send back "output" from low-code module as JSON
  res.json(output);

}

exports.run = app;

...