2017-07-17 68 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(); 
}