Example of a low-code module with a bug





**free

  Dcl-PR PJSCALL ExtPgm;
    ParmType        Char(30)          Const;
    ModuleID        VarUCS2(500)      Const;
    RoutineName     VarUCS2(70)       Const;
    InputParms      Char(16773104)    Const Options(*Varsize:*Omit:*NoPass);
    InputParmSize   Int(10)           Const Options(*Omit:*NoPass);
    OutputParms     Char(16773104)    Options(*Varsize:*Omit:*NoPass);
    OutputParmSize  Int(10)           Const Options(*Omit:*NoPass);
  End-PR;

  Dcl-DS InputDS Qualified Inz;
    custno  Zoned(4: 0);
  End-DS;

  Dcl-DS OutputDS Qualified Inz;
    name  VarChar(30);
  End-DS;

  InputDS.custno = 1234;			// pass input to the low-code module in "InputDS"

  Monitor;

    PJSCALL('*MODULE'
      : 'pjstips:pjstips_04_1.module.json'
      : 'routine1'
      : InputDS
      : %Size(InputDS)
      : OutputDS
      : %Size(OutputDS)
    );

  On-Error;


  EndMon;

  dsply  OutputDS.name;				// output from the low-code module in "OutputDS"		
  *inlr = *on;
  return;


2023-01-12 17:18:17 | Session 15A8388C315188097F933049F61E80008712FB286E1C92B80FF342A2E9F69167 | User TNGUYEN | Job TNGUYENAA | Job# 685033
null: TypeError: str.replace is not a function
message: "str.replace is not a function"
stack: "TypeError: str.replace is not a function
    at Querystring.rfc3986 (c:\Users\Tien Nguyen\Desktop\profoundjs\node_modules\request\lib\querystring.js:43:14)
    at Request.json (c:\Users\Tien Nguyen\Desktop\profoundjs\node_modules\request\request.js:1287:30)
    at Request.init (c:\Users\Tien Nguyen\Desktop\profoundjs\node_modules\request\request.js:406:10)
    at new Request (c:\Users\Tien Nguyen\Desktop\profoundjs\node_modules\request\request.js:127:8)
    at request (c:\Users\Tien Nguyen\Desktop\profoundjs\node_modules\request\index.js:53:10)
    at Object.profound.sendRequest (c:\Users\Tien Nguyen\Desktop\profoundjs\profoundjs\api\sendRequest.js:88:3)


Convert low-code module to "real" PJS code to debug

If you want to debug that low-code module, you can do the follow steps to "convert" it into "real" PJS code and call it as an Express Route (web service). This is also a good way to learn how things are done behide the scene. When a low-code module is called, the JSON file is "transformed" into "real" PJS code, and the that "transformed" module is run.



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 --------------------------
  // Consume REST service
  var _data = pjs.sendRequest({
    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 ----------------------------
  
  // send back "output" from low-code module as JSON
  res.json(output);

}

exports.run = app;


10. Click Save As, and save as pjstips_04_2.js.

11. In the Files tree, right-click on newly-created file pjstips_04_2.js, and click Properties. Mark the file as an Express Route, using HTTP method of "GET", with the Route Path as shown below.


Now, put that PJS module into debug, and call it as a web service, with this URL: http://localhost:8081/run/pjstips/pjstips_04_2 .

You'll get the same error as before. But now, you can debug it, and find out where the problem is. Is it due to the way you answered the low-code questions?  Is it due to a bug in one of the PJS functions, even though you've answered all the low-code questions properly? In this example, if you step through the code in debug, you'll see that the error occurs on the call to PJS API pjs.sendRequest(). The problem here is that the "body" of the POST request is sent as JSON, but the "headers" says that the content type is ""application/x-www-form-urlencoded", so the API pjs.sendRequest() fails. To fix that, you can comment out the "headers" in this code, and test it to verify that it works here.

After the "headers" line is commented out in this code, the call to URL http://localhost:8081/run/pjstips/pjstips_04_2 works, and JSON object "data" is returned to the browser as shown below.

Now that you know that in PJS real code, the culprit is the incorrect "headers", you can go back to the low-code module, remove that in the plugin "Consume REST service"; use the RPG program to call the modified low-code module, and it should work OK.