1

I have a javascript function in node js that queries a database and returns a json array to a callback function. That callback function then formats that json into a pure html table and saves that information to a variable. What would be the best way to get that html table into the jade template? I want to render that HTML table in the page.

function getData() {

var sql = require("mssql");
var dbConfig={
        server:"server",
        database: "db",
        user:"user",
        password: "pw"
}




        var conn = new sql.Connection(dbConfig);
        var req = new sql.Request(conn);
        conn.connect(function (err){
                if (err) {
                console.log(err);
                return;
                }


                req.query("SELECT * FROM table",resultsCallback)

                conn.close();
        });

}

function resultsCallback (err, recordset) {

        var tableify = require('tableify');

        if (err) {
                console.log(err);
        }
        else {

                var html = tableify(recordset);
                html = html.replace('<table>','');
                html = html.replace('</table>',');

        }
};
Sal
  • 625
  • 2
  • 7
  • 11

1 Answers1

2

Note: You don't need to change the return value of tableify, you just need to use != instead of = look at link : Rendering HTML in variable using Jade

While rendering your jade file, you can try passing the function in your route something like this:

-------- routes.js --------

router.get ( '/' , function(req,res){
    res.render(index,{
        table: getData()
    });
});

getData = function(){
    ...
}

-------- view.jade --------

!= table//rendering your table where table is of html format

!= will render the correct html, not a string

knkbga
  • 131
  • 8