jsreport-client dosn't work on server (TypeError: Cannot read property 'then' of undefined)



  • Hey everyone,

    Trying to generate PDF on server using jsreport-client to send request to another reporting server, following the example on https://jsreport.net/learn/nodejs-client

    JS report version 2.1.1

    const client = require("jsreport-client")("http://172.18.18.62");
    
    client.render({
        template: {
            content: "hello {{someText}}",
            recipe: "html",
            engine: "handlebars"
        },
        data: { someText: "world!!" }
    }).then((res) => {
        response.body((buffer) => {
            //prints hello world!!
            console.log(buffer.toString());
        });
    }); 
    

    However, this is what I'm seeing.

    W20180725-15:49:53.920(-4)? (STDERR) TypeError: Cannot read property 'then' of undefined
    W20180725-15:49:53.921(-4)? (STDERR)     at index.js (imports/startup/server/accounts/index.js:8:1)
    W20180725-15:49:53.921(-4)? (STDERR)     at fileEvaluate (packages/modules-runtime.js:343:9)
    W20180725-15:49:53.921(-4)? (STDERR)     at require (packages/modules-runtime.js:238:16)
    W20180725-15:49:53.921(-4)? (STDERR)     at index.js (imports/startup/server/index.js:1:14)
    W20180725-15:49:53.921(-4)? (STDERR)     at fileEvaluate (packages/modules-runtime.js:343:9)
    W20180725-15:49:53.922(-4)? (STDERR)     at require (packages/modules-runtime.js:238:16)
    W20180725-15:49:53.922(-4)? (STDERR)     at main.js (server/main.js:1:14)
    W20180725-15:49:53.922(-4)? (STDERR)     at fileEvaluate (packages/modules-runtime.js:343:9)
    W20180725-15:49:53.922(-4)? (STDERR)     at require (packages/modules-runtime.js:238:16)
    W20180725-15:49:53.923(-4)? (STDERR)     at 
    


  • Please try the code like this. There were some changes in the v2 that were not reflected in the mentioned article.

    const client = require('jsreport-client')('http://localhost:5488')
    
    async function render () {
      const res = await client.render({
        template: {
          content: 'hello {{someText}}',
          recipe: 'html',
          engine: 'handlebars'
        },
        data: { someText: 'world!!' }
      })
    
      const bodyBuffer = await res.body()
      console.log(bodyBuffer.toString())
    }
    
    render().catch(console.error)
    

    FYI the article is now updated



  • Hi @jan_blaha

    Thanks for your quick response, sad to say this, but I have literally copied your example to my project, and I'm getting different exception.

    W20180726-09:40:04.956(-4)? (STDERR) TypeError: Cannot read property 'body' of undefined
    W20180726-09:40:04.957(-4)? (STDERR)     at _callee$ (imports/startup/server/accounts/index.js:20:38)
    W20180726-09:40:04.957(-4)? (STDERR)     at tryCatch (/Users/user/Projects/meteor/project/node_modules/babel-runtime/node_modules/regenerator-runtime/runtime.js:62:40)
    W20180726-09:40:04.957(-4)? (STDERR)     at Generator.invoke [as _invoke] (/Users/dck/Projects/meteor/project/node_modules/babel-runtime/node_modules/regenerator-runtime/runtime.js:296:22)
    W20180726-09:40:04.958(-4)? (STDERR)     at Generator.prototype.(anonymous function) [as next] (/Users/dck/Projects/meteor/project/node_modules/babel-runtime/node_modules/regenerator-runtime/runtime.js:114:21)
    W20180726-09:40:04.958(-4)? (STDERR)     at step (/Users/dck/Projects/meteor/project/node_modules/babel-runtime/helpers/asyncToGenerator.js:17:30)
    W20180726-09:40:04.958(-4)? (STDERR)     at /Users/user/Projects/meteor/project/node_modules/babel-runtime/helpers/asyncToGenerator.js:28:13
    W20180726-09:40:04.958(-4)? (STDERR)     at /Users/user/.meteor/packages/promise/.0.10.0.ak8m4i++os+web.browser+web.cordova/npm/node_modules/meteor-promise/fiber_pool.js:43:40
    W20180726-09:40:04.958(-4)? (STDERR) /Users/user/Projects/meteor/project/node_modules/jsreport-client/lib/client.js:76
    W20180726-09:40:04.959(-4)? (STDERR)     cb(null, response)
    W20180726-09:40:04.959(-4)? (STDERR)     ^
    W20180726-09:40:04.959(-4)? (STDERR)
    W20180726-09:40:04.959(-4)? (STDERR) TypeError: cb is not a function
    W20180726-09:40:04.959(-4)? (STDERR)     at Request.<anonymous> (/Users/dck/Projects/meteor/project/node_modules/jsreport-client/lib/client.js:76:5)
    W20180726-09:40:04.960(-4)? (STDERR)     at emitOne (events.js:116:13)
    W20180726-09:40:04.960(-4)? (STDERR)     at Request.emit (events.js:211:7)
    W20180726-09:40:04.960(-4)? (STDERR)     at Request.onRequestResponse (/Users/user/Projects/meteor/project/node_modules/jsreport-client/node_modules/request/request.js:954:10)
    W20180726-09:40:04.960(-4)? (STDERR)     at emitOne (events.js:116:13)
    W20180726-09:40:04.960(-4)? (STDERR)     at ClientRequest.emit (events.js:211:7)
    W20180726-09:40:04.960(-4)? (STDERR)     at HTTPParser.parserOnIncomingClient [as onIncoming] (_http_client.js:551:21)
    W20180726-09:40:04.960(-4)? (STDERR)     at HTTPParser.parserOnHeadersComplete (_http_common.js:117:23)
    W20180726-09:40:04.961(-4)? (STDERR)     at Socket.socketOnData (_http_client.js:440:20)
    W20180726-09:40:04.961(-4)? (STDERR)     at emitOne (events.js:116:13)
    

    My last question would be, how can I save buffer response to PDF file?

    Thanks



  • What is the version of your jsreport-client package? You seems to have something old...
    https://github.com/jsreport/nodejs-client/releases

    The returned object from the render is promise which resolves into stream.
    I guess you know already or quickly find out how to write stream to file.

    The body function on the stream is just our helper, if you want to get a buffer from the stream.
    That can be also used to write to a file.



  • @jan_blaha updating the package fixed the problem, thanks very much



  • What it difference jsreport-browser-client-dist?


  • administrators

    @thearabbit jsreport-client is a package that should be consumed from server side javascript (node.js), it is a module that let you render reports from external server and get the result back in your server side code.

    jsreport-browser-client-dist is package that should be consumed from browser side javascript, it is a module that let you render reports from external server and get the result back in your browser side code.



  • Thanks I will try again


Log in to reply
 

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