开发者

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) {
      t开发者_运维知识库hrow 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 :)


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
- }


Even Better

ul
  - each result in results
  li= result.text


Thanks to generalhenry I've found a way.

Basically I do not need to add other stuff to the mysql get function or the query function in the server js.

It's all about the jade engine, this code works: take as input the variable that you are passing from the get function (in this case results) and iterate trough it, selecting the field that I want.

h1 Hello there!
p Welcome to this try
- for( var i = 0; i < results.length; i++ ) {
- var textbody = results[i]
    p= textbody['text']
- }

Hope might help others in the future :)


Like this. Results is an array so you have to loop over it

    for (var i in results){
         var sqlResult = results[i];
         console.log(sqlResult['text'], sqlResult['title']);
    }
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜