How to avoid request data array error?



  • Hi,

    I have a report which gets its data from a rest api. It is done with beforeRender.

    The rest api does not always send back data, sometimes the response is okay with an empty array because there is no new data. In this case I get an error from the report.

    "message": "Request data can not be an array. you should pass an object in request.data input"

    This is the function:

    function beforeRender(req, res, done) {
        require('request')({
            rejectUnauthorized: false, //Error: UNABLE_TO_VERIFY_LEAF_SIGNATURE
            url: 'https://somehost/api/v2/something',
            json: true,
            encoding: 'utf8',
            method: 'POST',
            headers: {
                'Authorization': 'Basic abc123',
                'X-DreamFactory-Api-Key': 'abc123',
                'Accept': 'application/json'
            },
            body: {
                "params": [{
                    "name": "loginin",
                    "param_type": "IN",
                    "value": req.data.loginin
                }],
                "wrapper": "data"
            }
        }, function(err, response, body) {
            if (err) {
                return done(err);
            }
            //req.template.content = JSON.stringify(body);
            req.data = body;
            //req.template.content = JSON.stringify(req.data);
            done();
        });
    }
    

    Unfortunately I cannot change the rest api. So I have to exit beforeRender if the response is a empty array. I tried some if statements, but non did work...

    Any Ideas, how to do this?



  • The data prop on the jsreport rendering request cannot be an array as the error states.
    You should pass an object for example like this and design your report correspondingly.

    req.data = { items: body };
    


  • Works perfect. Thx.


Log in to reply
 

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