Referencing Scripts in Same Folder with Postgres Storage



  • Hello,
    I'm curious if someone can help me reference (in js) a script in the same folder on JSReport v2.5.0
    I'm using a JSReport server set up by the service REDCap Cloud, so configuration of the server is hidden away from me.

    As far as I can tell no base path has been set up ('./' doesn't seem to reference the directory the script exists in), the entity storage is a Postgres SQL server.
    What full-path should I supply a "require" call to reference script files (i.e. path to data storage)?

    Any help would be greatly appreciated.
    Thank you!
    -Austin



  • Do you mean script inside <script> tag in the template content?

    You can create asset entity and then reference it
    https://jsreport.net/learn?version=2.11.0

    And then in the template use

    <script>
       {#asset ./myscript.js}
    </script>
    


  • @jan_blaha
    Thanks for the reply Jan.
    Yeah, I'm able to reference the asset but I'm still having issues.
    My script asset is using Highcharts JS code and my template includes in the html Head:
    "<script src="https://code.highcharts.com/highcharts.js"></script>
    <script src="https://code.highcharts.com/highcharts-more.js"></script>"

    However, when the asset script, referenced as you suggested, is run I receive the error (renderHospGraph is the function referencing Highcharts):
    "ReferenceError: Highcharts is not defined
    at renderHospGraph (evaluate-user-script.js:246:3)
    at evaluate-user-script.js:75:1
    at Script.runInContext (vm.js:133:20)
    at VM.run (/opt/jsreport/node_modules/vm2/lib/main.js:212:72)
    at run (/opt/jsreport/node_modules/jsreport-core/lib/render/safeSandbox.js:183:19)
    at module.exports (/opt/jsreport/node_modules/jsreport-scripts/lib/scriptEvalChild.js:170:5)
    at IncomingMessage.<anonymous> (/opt/jsreport/node_modules/script-manager/lib/worker-servers.js:268:47)
    at IncomingMessage.emit (events.js:203:15)
    at IncomingMessage.EventEmitter.emit (domain.js:466:23)
    at endReadableNT (_stream_readable.js:1145:12)"

    Any recommendations on the proper way to integrate Highcharts JS code from the asset script into the template?
    Thanks!



  • It works fine for me.
    https://playground.jsreport.net/w/anon/qaFmFJov

    Try to run with Debug button in your studio. Maybe you have some js error visible there.



  • @jan_blaha
    Okay, I got it to work actually. Thank you!

    I was also wondering if you had advice about reading from sample data in JSON format?
    After importing my script asset, I'm having trouble trying to pass sample data to a function within the asset.
    0_1646943827194_jsreportError.png

    My sample data looks like:
    {
    "quarters": [{
    "name": "Q1 2016",
    "patientData": [
    {"dup1": 90},
    {"dup1": 30},
    {"dup1": 20}
    ],
    "totalSize": 5,
    "included": 3
    },
    {
    "name": "Q2 2016",
    "patientData": [
    {"dup1": 200},
    {"dup1": 93},
    {"dup1": 100}
    ],
    "totalSize": 7,
    "included": 3
    }]
    }

    Thanks!



  • @jan_blaha
    I'm kind of a Javascript novice so really any help/teaching is greatly appreciated. I'm at the point where I can take the request data via beforeRender in a script and push this data to an array in my JSON sample data.
    My sample data now looks like this:
    { quarters:
    [ { name: 'Q1 2016',
    patientData: [Array],
    totalSize: 5,
    included: 3 },
    { name: 'Q2 2016',
    patientData: [Array],
    totalSize: 7,
    included: 3 } ],
    vizData:
    [ [ 100, 3, 5, 43.84939195509823, 100 ],
    [ 0, 3, 7, 0, 56.15060804490177 ] ] }

    My issue now with using Highcharts visualizations. I'd like to read the vizData from the JSON and render a Highcharts graph with it. I have a function renderDupGraph in one of my assets that if passed vizData, can find the Highchart's library, and can find render <div id='container'> in template that it will create the graph.

    Is it possible to either:

    • Get vizData array with Handlebars syntax as an argument to this Highcharts function inside the template <script> tag?
      • Additionally, this call needs to happen in template so that the Highcharts package can be found from the template <head> and also to find the target <div id='container'>.

    OR

    • Import Highcharts into my script and perform the call in an afterRender function?
      • Here I would get data from req.data in afterRender

    Thank you for any guidance!
    -Austin



  • This problem and the solution is described here

    https://jsreport.net/blog/using-input-data-in-html-page-inline-javascript



  • @jan_blaha
    Awesome Jan, I have things working now. Thank you for your help!
    -Austin


Log in to reply
 

Looks like your connection to jsreport forum was lost, please wait while we try to reconnect.