pass variables to EJS view of separate queries

Asked

Viewed 1,199 times

3

I’m starting with Node/express etc. Create a website with the intention of learning and I’m in need of 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
      });
    });
});

Use this variable to make the Binding date with a table (SAPIU5 component)

I want on the same page, IE, index use another variable to fill another table. I’m using the same logic, but it’s 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 both at the same time does not work.

I’ve tried a few ways to solve this problem, but I couldn’t.

Note, why are using specific components in the EJS view (I am using SAPUI5) I need to have the result in two variables, because this way I can make the databind for the components (tables). At this moment only has a table, I could not have the two 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 query sql variable is to use it in the view along with another variable of an API query.

Thank you very much.

1 answer

1


You need to string those two asynchronous methods together to just call out the .render when you have both answers. You can do it in different ways.

I’ve talked about it in other responses, suggested with Promises, with the async module or with handmade manager.

In your case since it’s only two callbacks you can do so:

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)
            });
        });
    });
});
  • 1

    Sergio, thank you very much. You have no idea how I have researched, studied callback, closure etc, to try to get it done. There is still something missing for this idea of callback to enter my mind. rsrs. I will continue studying the theory. Thank you again. Really thank you, you have no idea how much I tried before posting. Thank you :)

  • @Douglascorrea great! I’m happy to help :) If you want you can mark the answer as accepted.

  • 1

    @ Sergio, Helped me a lot, I lost several hours searching before posting here. About marking the answer with the accepted, I didn’t even know it.I’m new here. But it’s already been checked. Again my many thanks. ;)

  • @Great douglascorrea. If you want to understand better I gave an even more comprehensive answer here: http://answall.com/a/140883/129

Browser other questions tagged

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