Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Retrieving all the content of a MySQL query to Jade template engine through NodeJS and Express

Simple newbie question, I am starting out with nodejs, and I am pretty new to backend languages in general.

I managed to publish a single field from a database to a webpage using the default jade engine in express-js.

/**
 * Module dependencies.
 */

var express = require('express');

var app = module.exports = express.createServer();
var sqlResult;
//MySql
var mysqlClient = require('mysql').Client,
    newClient = new mysqlClient(),
    Database = 'test',
    Table = 'test_table';

    newClient.user ='root';
    newClient.password='password';
    newClient.connect(console.log('connected to the database.'));
    newClient.query('USE '+Database);
    newClient.query(
  'SELECT * FROM '+Table,
  function selectCb(err, results, fields) {
    if (err) {
      throw err;
    }
    sqlResult = results[0];
    console.log(sqlResult['text'], sqlResult['title']);
  }
);
// Configuration

app.configure(function(){
  app.set('views', __dirname + '/views');
  app.set('view engine', 'jade');
  app.use(express.bodyParser());
  app.use(express.methodOverride());
  app.use(express.cookieParser());
  app.use(express.session({ secret: 'your secret here' }));
  app.use(app.router);
  app.use(express.static(__dirname + '/public'));
});

app.configure('development', function(){
  app.use(express.errorHandler({ dumpExceptions: true, showStack: true })); 
});

app.configure('production', function(){
  app.use(express.errorHandler()); 
});

// Routes

app.get('/', function(req, res){
  res.render('index', {
    title: sqlResult['title']
  });
});

app.listen(3000);
console.log("Express server listening on port %d in %s mode", app.address().port, app.settings.env);

My question is, how can I show a list of all the elements retrieved by theMySQL Query?

Thank you :)

like image 422
Dave Avatar asked Aug 02 '11 16:08

Dave


People also ask

How do I get query results in node JS?

example.js con. connect(function(err) { if (err) throw err; // if connection is successful con. query("UPDATE students SET marks=84 WHERE marks=74", function (err, result, fields) { // if any error while executing above query, throw error if (err) throw err; // if there is no error, you have the result console.

Is Jade template engines can be used with node js?

Jade is a template engine for node. js and the default rendering engine for the Express web framework. It is a new, simplified language that compiles into HTML and is extremely useful for web developers. Jade is designed primarily for server-side templating in node.


2 Answers

Pass the full results to Jade

app.get('/', function(req, res){
  newClient.query('USE '+Database);
  newClient.query('SELECT * FROM '+Table, function selectCb(err, results, fields) {
    if (err) {
      throw err;
    }
    res.render('index', {
      title: results[0].title,
      results: results
    });
  }
});

Then iterate over them inside your Jade

- for( var i = 0, len = results.length; i < len; i++ ) {
  .result
    .field1= results[i].field1
    .field2= results[i].field2
- }
like image 190
generalhenry Avatar answered Oct 07 '22 23:10

generalhenry


Even Better

ul
  - each result in results
  li= result.text
like image 23
Dave Avatar answered Oct 07 '22 22:10

Dave