T
T
Tolych2015-09-18 11:57:35
Node.js
Tolych, 2015-09-18 11:57:35

Nodejs Express how to display required query result in html?

Good afternoon!
I am writing a web server from scratch, which, based on the results of a post request, should produce a result on the page.
To do this, I installed nodejs, connected express, express-generator, and a module for communicating with the MSSQL database, mssql.
Through the generator ( expressjs.com/starter/generator.html ) I created the application layout.
In the model, I created the db.js file for accessing the database and executing a stored procedure with parameters.

var sqlDb = require('../node_modules/mssql');
var settings = require('./settings');

exports.selectOubySummBuking = function(mon, tue, wed, thu, fri, sat, sun, buksumm, sql, callback){

  var conn = new sqlDb.Connection(settings.dbConfig);
  conn.connect()
  .then(function(){
      var req = new sqlDb.Request(conn);
      //req.verbose = true;
      req.input('mon', mon);
      req.input('tue', tue);
      req.input('wed', wed);
      req.input('thu', thu);
      req.input('fri', fri);
      req.input('sat', sat);
      req.input('sun', sun);
      req.input('buksumm', buksumm);
      req.execute(sql)
      .then(function(data){
        callback(data[0]);
      })
      .catch(function(err){
        console.log(err);
        callback(err);
      });
  })
  .catch(function(err){
      console.log(err);
      callback(err);
  });
};

In the router, in the index.js file, I described the post request handler
var express = require('express');
var dbconnect = require('../model/db.js');
var outlets = require('../controller/outlet.js');
var router = express.Router();

// определяем метод который будет обрабатывать обращение
router.post('/oudb', function(req, res){
  var allBuking = req.body.allBuking;
  dbconnect.selectOubySummBuking(2, 2, 3, 4, 5, 5, 3, allBuking, "selectOubySummBuking", function(data) {
    res.render('index', {
              title: "Outlet DB",
              results: data
    });
  });
});


module.exports = router;

The result is displayed through the ejs template engine, the code of the index.ejs file
<!DOCTYPE html>
<html lang = "ru">
  <head>
    <meta charset="utf-8">
    <title>My app</title>
    <link rel="stylesheet" href="bootstrap/dist/css/bootstrap.css">
    <script src = "jquery/dist/jquery.js"></script>
    <script src = "bootstrap/dist/js/bootstrap.js"></script>
  </head>
  <body>
    <form action="/oudb" method="post">
      <input name="allBuking" type="number">
      <input type="submit">
    </form>
    <ul>
        <% for(var i=0; i<results.length; i++) {%>
       <li><%= results[i].City %></li>
        <% } %>
    </ul>
  </body>
</html>

If the stored procedure returns a certain number of rows, then the page is displayed correctly. since as a result of rendering, we passed values ​​to results, but if the procedure does not return a result, how to make it so that the phrase "result not found, change query parameters" is displayed on the same page ???

Answer the question

In order to leave comments, you need to log in

1 answer(s)
P
Pavel, 2015-09-18
@Tolych

I'm not familiar with ejs ... but maybe something like this:

<ul>
  <% results.forEach(function(item){ %>
    <li><%= item.City %></li>
  <% }); %>
<% if (results.length == 0) { %>
  <li>результат не найден, измените параметры запроса</li>
<% } %>
</ul>

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question