Format JSON in EJS

Asked

Viewed 708 times

1

hello personal I am having a problem 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 own a webservice Rest that returns me purchase orders with several items but summarizing would be something like this

{ "nrSolicCompra":"94705", "requester":"Jsilva", "items":{"cd material":"259560", " dsMaterial":"Flat head nails", "qtMaterial":"1000" } }

I need to pick up this json and show in front end (which is in EJS nodejs)

currently have the following structure.

a page called index.js where I have the following code that 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 the test page.ejs I can set this code to print json

<%- JSON.stringify(solicitacaoCompra) %>

and it prints out in string what I need but I’m not able to save it in a variable or make a format in html to stay in 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 view would appear a modal with the items of that request.

  • Can you put the JSON that the webservice gives? The example you put on top is poorly formatted and I’m not sure if it’s an array or an object.

  • of course is this {"nrSolicCompra":"94705","requester":"Jsilva","items":{"cdMaterial":"259560","dsMaterial":"flat head nails","qtMaterial":"1000"}}

2 answers

0

basically the problem I was having was with the generated json.

I started using the Jackson framework and the problem was solved without change in the code.

webservice is done in java with Maven, so to install Jackson I only had to add the code to pom.xml. org.codehaus.Jackson Jackson-jaxrs 1.9.13

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 table names and the other the values of each property of that object.

  • ola Thank you very much for the help, this I tried but instead of it print the data in the browser it prints [Object Object],[Object Object],[Object Object]

  • @user2913044 I have nowhere to test EJS, but doing it by hand to be sure, test like this: https://jsfiddle.net/yLzmxm94/

  • I think there must be something wrong on the index.js page.js pq looks like the test.ejs page does not recognize json Object only when I convert to string

Browser other questions tagged

You are not signed in. Login or sign up in order to post.