通过 NodeJS 和 Express 将 MySQL 查询的所有内容检索到 Jade 模板引擎
简单的新手问题,我是从 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
将完整结果传递给 Jade
然后在 Jade 中迭代它们
Pass the full results to Jade
Then iterate over them inside your Jade
更好
Even Better
感谢亨利将军,我找到了办法。
基本上我不需要在服务器js中的mysql get函数或查询函数中添加其他东西。
这都是关于 jade 引擎的,这段代码的工作原理是:将从 get 函数传递的变量(在本例中为结果)作为输入,并对其进行迭代,选择我想要的字段。
希望将来可以帮助其他人:)
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.
Hope might help others in the future :)
像这样。 Results 是一个数组,所以你必须循环它
Like this. Results is an array so you have to loop over it