Formatting JSON in EJS

1

I have a problem with using nodejs and express, I have never used this language and I do not have much knowledge of it so I will try to be as specific as possible here.

1 I have a webservice rest that returns me purchase orders with several items but summarizing it would be something like this

{ "nrSolicCompra": "94705", "applicant": "JSilva", "items": {"cd Material": "259560",          "DsMaterial": "flat head nails",          "qtMaterial": "1000"         } }

I need to get this json and it shows in the front end (which is in EJS nodejs)

I currently have the following structure.

A page called index.js where I have the following code I found on the net:

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

router.get('/teste', function(req, res, next) {
    res.render('teste', { variavel: 'passou no teste', solicitacaoCompra: solicitacaoCompra })
});

var http = require('http');

var options = {
    host: 'localhost',
    port: 8080,
    path: '/meuWebService/metodoRetorno',
    mothod: 'GET'
};

var solicitacaoCompra;

http.request(options, function(res) {
    var body = "";

    res.on('data', function(chunk) {
        body += chunk;
    });

    res.on('end', function() {
        solicitacaoCompra = JSON.parse(body);
    })
}).end();

module.exports = router;

on page teste.ejs I can put this code in order to print json

<%- JSON.stringify(solicitacaoCompra) %>

and it prints in string what I need but I'm not able to store that in a variable or do a html formatting to get on a perfect list something like

<table>
  <th> numero pedido</th>
  <th> solicitante</th>
</table>

The initial table would be just that and when the user clicks on a button called visualization it would appear a modal with the items of that request.

    
asked by anonymous 06.01.2017 / 14:47

2 answers

0

I think what you need is:

<table>
  <% for(var i = 0, colunas = Object.keys(solicitacaoCompra); i < colunas.length; i++) { %>
     <tr>
       <th><%= colunas[i] %></th>
     </tr>
  <% } %>
    <tr>
      <% for(var i = 0, colunas = Object.keys(solicitacaoCompra); i < colunas.length; i++) { %>
       <td><%= solicitacaoCompra[colunas[i]] %></td>
    <% } %>
  </tr>
</table>

So you have two loops, one that iterates the names of the tables and another the values of each property of that object.

    
06.01.2017 / 16:36
0

Basically the problem I was having was with json generated.

I started using the jackson framework and the problem was solved without changing the code.

webservice is done in java with maven, so to install jackson I just had to add the code to pom.xml.             org.codehaus.jackson             jackson-jaxrs             1.9.13         

    
16.01.2017 / 14:43