2017-07-17 70 views
0

我想使jsreport在Azure功能應用程序中工作。我已經安裝了所有需要的軟件包,它們是jsreport-core jsreport-render jsreport-phantom-js,它們似乎都工作得很好。我的代碼:Azure功能應用jsreport在HTTP觸發器不工作

module.exports = function (context, req) { 
context.log('JavaScript HTTP trigger function processed a request.'); 

if (req.query.content || (req.body && req.body.content)) { 
    var pdf = renderPdf(req.query.content || req.body.content, {}) 
    context.res = { 
     status: 200, 
     body: { data: pdf } 
    }; 
} 
else { 
    context.res = { 
     status: 400, 
     body: "Please pass the content on the query string or in the request body" 
    }; 
} 
context.done();}; 

function renderPdf(content, data){ 
var jsreport = require('jsreport-core')(); 

var promise = jsreport.init().then(function() { 
    return jsreport.render({ 
     template: { 
      content: content, 
      engine: 'jsrender', 
      recipe: 'phantom-pdf' 
     }, 
     data: data 
    }); 
}); 
return Promise.resolve(promise);} 

我用這篇文章作爲一個例子:Export html to pdf in ASP.NET Core

我的最終目標是調用從asp.net核心這個功能。謝謝您的幫助。

+0

那麼,您面臨的問題是什麼? – Mikhail

+0

基本上函數只是返回這個{「data」:{}} @Mikhail –

回答

2

您的renderPdf函數返回一個承諾,您沒有正確使用。您不能只將承諾分配給結果主體,而是將主體分配給then

if (req.query.content || (req.body && req.body.content)) { 
    renderPdf(...).then(pdf => { 
     context.res = { 
      status: 200, 
      body: { data: pdf } 
     }; 
     context.done(); 
    }); 
} 
else { 
    context.res = { 
     status: 400, 
     body: "Please pass the content on the query string or in the request body" 
    }; 
    context.done(); 
}