json file, get data from script



  • Hi, I want to ask if it is possible to get data -json file from the script? I have json file and I want to get the json data via javascript... is it possible? if it is possible, how can I do it?

    or is there any better suggestion to do this?

    Thanks in advance



  • Hi, sure....

    one option is to make a custom handlebars helper with

    function foo() {
        return require('fs').readFileSync('package.json')
    }
    

    another option is to create custom script, read file there and pass it through data to the template rendering.
    Read file the same way as in previous code.



  • Oh thank you,
    how can I pass through it to data?
    I can't understand this one, so it creates data?



  • It is common use case to use script to read data and pass it to the template rendering. See the docs here
    https://jsreport.net/learn/scripts

    In your use case it would be something like

    function beforeRender(req, res, done) {
      require('fs').readFile('package.json', (err, res) {
        if (err) {
           return done(err)
        }
    
        req.data = Object.assign({}, req.data, JSON.parse(res.toString())
        done()
      }) 
    }


  • ah, thank you !
    it seems I kind of understand now, thanks :D



  • Thank you , this is my code for anyone with same problem :
    This is work for me

    • Javascript :
    const path = require('path')
    const fs = require('fs')
    
    function beforeRender(req, res, done) {
                                   //file location, file name
      const configPath = path.join("E:\\Program\\", 'sample.json')
    
      // any message called with console.log, console.warn, console.error will be saved into logs
      console.log('reading custom file..')
      //testing file location
      console.log('from : '+configPath)
     
     //read file json
      fs.readFile(configPath, (err, content) => {
        if (err) {
          return done(err)
        }
    
        try {
          req.data = Object.assign({}, req.data, JSON.parse(content.toString()))    
    /*      const myconfig = JSON.parse(content.toString())
    
          // use config values to apply some conditional logic in the script
          if (myconfig.validateData) {
            // .. your custom logic here ..
          }
    
          // or pass custom config as data to your templates
     
          req.data.myconfig = myconfig
    */
          done()
        } catch (e) {
          done(new Error('error while trying to parse custom config at ' + configPath + ': ' + e.message))
        }
      })
    }
    
    • jsreport.config.json
    {
      "extensions": {
        "authentication": {
          "cookieSession": {
            "secret": "<your strong secret here>"
          },
          "admin": {
            "username": "sa",
            "password": "Password1000"
          },
          "enabled": false
        },
        "scripts": {
          "timeout": 40000,
          "strategy": "http-server",
    	  "allowedModules": ["path","fs"]
    	  
        },
        "sample-template": {
          "createSamples": true
        }
      },
      "httpPort": 5488,
      "store": {
        "provider": "fs"
      },
      "blobStorage": {
        "provider": "fs"
      },
      "logger": {
        "console": {
          "transport": "console",
          "level": "debug"
        },
        "file": {
          "transport": "file",
          "level": "info",
          "filename": "logs/reporter.log"
        },
        "error": {
          "transport": "file",
          "level": "error",
          "filename": "logs/error.log"
        }
      },
      "allowLocalFilesAccess": true,
      "templatingEngines": {
        "timeout": 10000,
        "strategy": "http-server"
      },
      "chrome": {
        "timeout": 40000
      }
      
    }
    

Log in to reply
 

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