2016-07-22 62 views
0

這是使用jsreport生成報告的代碼。我想通過將輸入參數傳遞給jsreport來生成報告。如何將參數傳遞給jsreport以基於參數生成報告

var express= require('express'); 
var router = express.Router(); 
var request=require('request'); 

router.get('/' ,function(req,res,next){ 
var shortid=req.query.shortid; 
var preview =req.query.preview; 

var data={ 
template:{'shortid':shortid , "recipe" : "html"}, 
options:{ 
    preview:preview 
} 
} 
var options={ 
uri:'http://localhost:5488/api/report', 
method:'post', 
json:data 
//how to pass parameter here like uri,method. 
} 
request(options).pipe(res); 
}); 
module.exports=router; 

我要傳遞的參數在jsreport的腳本MySQL查詢。

回答

0

您可以通過輸入數據傳遞參數或者將其固定選項屬性

router.get('/' ,function(req,res,next){ 
    var shortid=req.query.shortid; 
    var preview =req.query.preview; 

    var data={ 
    template:{'shortid':shortid , "recipe" : "html"}, 
    options:{ 
     preview:preview 
    }, 
    data: { 
     paramA: 'foo' 
    } 
    } 
    var options={ 
    uri:'http://localhost:5488/api/report', 
    method:'post', 
    json:data 
    //how to pass parameter here like uri,method. 
    } 
    request(options).pipe(res); 
}); 

然後你就可以在req.data.paramA達到腳本參數。渲染報告中的錯誤 - (如果使用jsreport < 1.0,達到其全球request對象)

function beforeRender(req, res, done) { 
    var paramA = req.data.paramA 
    ... 
    done() 
} 
+0

我得到一個錯誤..'data'未定義 – Abhi

+0

在jsreport執行模板時,我收到以下錯誤'時出錯:evalmachine。 :28 var param = req.param.paramA; ^ 無法讀取未定義的屬性'paramA' 堆棧 - 錯誤:evalmachine。 :28 var param = req.param.paramA; ^' –

+0

在我的示例 –

相關問題