M
M
Magnus Keef2018-12-12 01:32:49
Node.js
Magnus Keef, 2018-12-12 01:32:49

How to display data from database to html page?

I use Node js to create an application

application screenshot
5c1037e808bd7994855938.png

the database is on a local sql server and has several tables. In the figure above, the places where the data from the database should be are highlighted in red.
I connect to the database and can (even) display the whole
table
5c10390772c83788692702.png
via
here is such a script
var Connection = require('tedious').Connection;
var Request = require('tedious').Request;
var express = require('express');

// Create connection to database
var config = {
    server: "localhost",
    userName: 'bassb',
    password: '32460101',
    //driver: "msnodesqlv14",
    options: {
        database: "GIBDD",
        encrypt: false
    }
};
var connection = new Connection(config);

connection.on('connect', function (err) {
        if (err) {
            console.log(err);
        }
        else {
            queryDatabase();

        }
    }
)
//в этой функции я делаю select таблицы и вон, 
//где "request.on" вывожу в консоль, а мне надо выводить на страницу!
function queryDatabase() {
    request = new Request("SELECT * FROM officers", function (err, rowCount, rows) {
        console.log(rowCount + ' row(s) returned');
        //process.exit();
    });

    request.on('row', function (columns) {
        columns.forEach(function (column) {
            console.log("%s\t%s", column.metadata.colName, column.value);
            colOfficer.push(column.metadata.colName);
            valOfficer.push(column.value);
        });
    });

    connection.execSql(request);
}

There is an idea: to enter the data from the database into an array or an object and already transfer it to the page through a template engine (I use ejs, because I came across it first).
THE QUESTION IS HOW TO WRITE DATA FROM THE DB INTO AN ARRAY OR OBJECT, so that later it can be displayed on the page with its help ???
Maybe there is some other option? Please show an example of how to transfer data from the database to the page.
Connection code taken from here

Answer the question

In order to leave comments, you need to log in

2 answer(s)
M
Magnus Keef, 2018-12-15
@SecurityYourFingers

var mass = [];

function queryDatabasePunish() {
    request = new Request(`SELECT punishments.num_punishment, breakdowns.coast, punishments.name_breakdown FROM breakdowns INNER JOIN punishments ON breakdowns.num_breakdown = punishments.num_breakdown WHERE (punishments.num_ptc = '${password}')`, function (err, rowCount, rows) {
        console.log(rowCount + ' row(s) вернулось из штрафов');
        
        PunCount = rowCount;
    });
    request.on('row', function (columns) {
        columns.forEach(function (column) {
            var rep = column.value;
                mass.push(rep);
                //console.log(mass);
        });
    });

    abcdef.execSql(request);
}

K
Kirill Kudryavtsev, 2018-12-12
@Deissh

When rendering a template, you can pass parameters, data. After that, through the symbols <%= and %>, you can write data to the template. More information can be found on the official website of EJS

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question