pass variables to view EJS from different queries

2

I'm starting with node / express etc. Create a site with the intention of learning and I need help.

I have a mysql query that is sent to an EJS view via the code below:

app.get('/', function (req, res) {
  connection.query('SELECT actor_id, first_name, last_name, last_update FROM actor',
     function (error, results, fields) {
      res.render('index', { title: 'Render by app.get',
        datasetresult  : results
      });
    });
});

I use this variable to do the data binding with a table (SAPIU5 component)

I want on the same page, that is, index use another variable to populate another table. I am using the same logic but it is not working

var Client = require('node-rest-client').Client;
var client = new Client();
client.get("http://api.randomuser.me/",
  function (data, response) {
    data = JSON.stringify(data.results),
       res.render('index', {
         datasetapi : data
       });
    });
});

If I use one of the two works the two at the same time does not work.

I've tried some ways to solve this problem, but I could not.

Note, because you are using specific components in the EJS view (I am using SAPUI5) I need to have the result in two variables, so I can do the databind for the components (tables). At this point I only have one table, I could not have both variables with data.

<!DOCTYPE html>
<html>
  <head>

    <meta http-equiv='X-UA-Compatible' content='IE=edge' />
    <meta http-equiv="Content-Type" content="text/html;charset=UTF-8"/>

    <script id='sap-ui-bootstrap'
      <script src='https://openui5.hana.ondemand.com/resources/sap-ui-core.js'
      data-sap-ui-theme='sap_goldreflection'
      data-sap-ui-libs='sap.ui.commons, sap.ui.table'>
    </script>

    <script src="https://code.jquery.com/jquery-3.1.0.min.js"integrity="sha256-cCueBR6CsyA4/9szpPfrX3s49M9vUU5BgtiJj06wt/s="
            crossorigin="anonymous">
    </script>

    <title></title>

    <script type="text/javascript">

    var oMysql = <%-JSON.stringify(datasetresult)%>;

    var oApi   = <%-JSON.stringify(datasetapi)

    var oTable = new sap.ui.table.Table({
             width: "1000px",
             visibleRowCount: 10
         });
         oTable.setTitle("Actor Details");

         oTable.addColumn(new sap.ui.table.Column({
             label: new sap.ui.commons.Label({ text: "Id" }),
             template: new sap.ui.commons.TextView().bindProperty("text", "actor_id"),
               width: "70px"
         }));

         oTable.addColumn(new sap.ui.table.Column({
             label: new sap.ui.commons.Label({ text: "First Name" }),
             template: new sap.ui.commons.TextView().bindProperty("text", "first_name"),
             sortProperty: "first_name",
               //filterProperty: "lastName",
               width: "230px"
         }));

         oTable.addColumn(new sap.ui.table.Column({
             label: new sap.ui.commons.Label({ text: "Last Name" }),
             template: new sap.ui.commons.TextView().bindProperty("text", "last_name"),
             width: "230px"
         }));

         //Create a model and bind the table rows to this model
         var oModel = new sap.ui.model.json.JSONModel();
         oModel.setData({modelData: oMysql});
         oTable.setModel(oModel);
         oTable.bindRows("/modelData");
         oTable.placeAt("content");
    </script>
  </head>


  <body class="sapUiBody" role="application">
   <div id="content"></div>
 </body>
</html>

How do I generate a sql query variable is to use it in the view along with another variable from an API query.

Thank you very much.

    
asked by anonymous 14.07.2016 / 07:09

1 answer

1

You have to chain these two asynchronous methods to just call .render when you have both answers. You can do this in different ways.

I've already talked about this in other answers, I suggested with Promises , with # or hand-held manager a>.

In your case as there are only two callbacks you can do this:

var Client = require('node-rest-client').Client;
var client = new Client();

app.get('/', function(req, res) {
    client.get("http://api.randomuser.me/", function(data, response) {
        connection.query('SELECT actor_id, first_name, last_name', function(error, results, fields) {
            res.render('index', {
                title: 'Render by app.get',
                datasetresult: results,
                datasetapi: JSON.stringify(data.results)
            });
        });
    });
});
    
14.07.2016 / 10:23