Reputation: 12465
I could successfully get data from the following script and show in report.
function beforeRender(done){
require("request")({url:"http://nicolas.kruchten.com/pivottable/examples/mps.json",json:true},function(err, response, body){
request.data = {posts:body};
done();
})
}
I used this,
<h1>Request</h1>
<table style='border-style:solid'>
<tr>
<th>Province</th>
<th>Party</th>
<th>Name</th>
</tr>
{{for posts}}
<tr>
<td>{{:Province}}</td>
<td>{{:Party}}</td>
<td>{{:Name}}</td>
</tr>
{{/for}}
</table>
But when I call a REST api using following query it gets data and writes in console but does not show in report.
var request = require('request');
function beforeRender(done){
require('request')({headers: {
'sToken': 'qq',
'log': 'dd'
}, url: 'http://localhost:3000/com.dd.com/Inventory' , json:true },
function(error, response, body) {
request.data = {posts:body};
console.log(request.data );
done();
});
}
HTML code
<h1>Request</h1>
<table style='border-style:solid'>
<tr>
<th>Name</th>
</tr>
{{for posts}}
<tr>
<td>{{:Name}}</td>
</tr>
{{/for}}
</table>
JSON string returned:
{ posts:
{ Id: '700',
Name: 'myName',
__osHeaders:
{ Version: '{EFF95F4C-2FA2-11E5-BA94-040150C75001}',
Namespace: 'com.dd.com',
Class: 'Inventory',
Tenant: '123',
LastUdated: '2015-07-21 08:20:56.197248204 -0400 EDT' } } }
Engine used: jsrender
Recipe: phantom-pdf
How can I display the values in Report? Where have I gone wrong when using the API?
Upvotes: 4
Views: 3816
Reputation: 12465
I found the issue. I commented out the following line which caused me not to print it on the report.
var request = require('request');
Therefore the new code is,
//var request = require('request');
function beforeRender(done){
require('request')({headers: {
'sToken': 'qq',
'log': 'dd'
}, url: 'http://localhost:3000/com.dd.com/Inventory' , json:true },
function(error, response, body) {
request.data = {posts:body};
console.log(request.data );
done();
});
}
Upvotes: 2
Reputation: 3095
The first mentioned API returns an array which then works with jsrender for
loop.
But according to your posted JSON string the second API returns a single object and that is why the for
loop doesn't work there. It would work without for loop with: <h1>{{:posts.Name}}</h1>
So the solution is to change the API call to actually receive an array or convert the output to it so you can loop over.
Upvotes: 0