通过 NodeJS 和 Express 将 MySQL 查询的所有内容检索到 Jade 模板引擎

发布于 2024-11-27 16:08:23 字数 1628 浏览 0 评论 0原文

简单的新手问题,我是从 Nodejs 开始的,而且我对后端语言总体来说还很陌生。

我设法使用express-js中的默认jade引擎将单个字段从数据库发布到网页。

/**
 * 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);

我的问题是,如何显示 MySQL 查询检索到的所有元素的列表?

谢谢 :)

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 :)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

别靠近我心 2024-12-04 16:08:23

将完整结果传递给 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
    });
  }
});

然后在 Jade 中迭代它们

- for( var i = 0, len = results.length; i < len; i++ ) {
  .result
    .field1= results[i].field1
    .field2= results[i].field2
- }

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
- }
假扮的天使 2024-12-04 16:08:23

更好

ul
  - each result in results
  li= result.text

Even Better

ul
  - each result in results
  li= result.text
染年凉城似染瑾 2024-12-04 16:08:23

感谢亨利将军,我找到了办法。

基本上我不需要在服务器js中的mysql get函数或查询函数中添加其他东西。

这都是关于 jade 引擎的,这段代码的工作原理是:将从 get 函数传递的变量(在本例中为结果)作为输入,并对其进行迭代,选择我想要的字段。

h1 Hello there!
p Welcome to this try
- for( var i = 0; i < results.length; i++ ) {
- var textbody = results[i]
    p= textbody['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 :)

微暖i 2024-12-04 16:08:23

像这样。 Results 是一个数组,所以你必须循环它

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

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']);
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文