我想寻求有关在node.js Jade中显示mySQL记录的帮助。我已经遵循了node.js Jade的基本教程并且能够通过显示默认的index.jade来使其工作,但是当我开始在我的index.js中实现mySQL连接链接和函数时,它显示无,并且在浏览器上没有显示数据。希望你们能尽快帮我解决这个问题,先谢谢你们。
这是我的app.js:
var express = require('express');
var path = require('path');
var favicon = require('static-favicon');
var logger = require('morgan');
var cookieParser = require('cookie-parser');
var bodyParser = require('body-parser');
var index = require('./routes/index');
var users = require('./routes/users');
var app = express();
// view engine setup
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'ejs');
app.set('port', process.env.PORT || 3000);
app.use(favicon());
app.use(logger('dev'));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded());
app.use(cookieParser());
app.use(express.static(path.join(__dirname, 'public')));
app.use('/', index);
app.use('/users', users);
/// catch 404 and forward to error handler
app.use(function(req, res, next) {
var err = new Error('Not Found');
err.status = 404;
next(err);
});
/// error handlers
// development error handler
// will print stacktrace
if (app.get('env') === 'development') {
app.use(function(err, req, res, next) {
res.status(err.status || 500);
res.render('error', {
message: err.message,
error: err
});
});
}
// production error handler
// no stacktraces leaked to user
app.use(function(err, req, res, next) {
res.status(err.status || 500);
res.render('error', {
message: err.message,
error: {}
});
});
module.exports = app;
var server = app.listen(app.get('port'), function() {
console.log('Listening on port %d', server.address().port);
});
这是我的路线/ index.js:
var express = require('express');
var router = express.Router();
var mysql = require('mysql');
var conn = {
host: 'localhost',
user: 'user',
password: 'password',
database: 'rest_api',
port: '3306'
};
/* GET home page. */
router.get('/', function(req, res) {
exports.index = function(req, res) {
var connection = mysql.createConnection(conn);
var query ='SELECT sensorId, sensors, pinNo, sensorsType FROM sensorInfo';
connection.query(query, function(err, rows, fields) {
if(err) throw err;
res.render('index', { title: 'Sensor REST API', 'items': rows });
});
};
});
module.exports = router;
这是我的/views/index.jade:
extends layout
block content
h1= title
p Welcome to #{title}
table
tr
th Sensor ID
th Sensors
th Pin Number
th Sensors Type
-each item in items
tr
td=item.sensorid
td=item.sensors
td=item.pinNo
td=item.sensorsType
这是我的sensorInfo结构:
CREATE TABLE IF NOT EXISTS `sensorInfo` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`sensorId` int(11) NOT NULL,
`sensors` text COLLATE utf8_unicode_ci NOT NULL,
`pinNo` int(11) NOT NULL,
`sensorsType` text COLLATE utf8_unicode_ci NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci AUTO_INCREMENT=4 ;
这是我的记录,我想从我的sensorInfo中显示出来
--
-- Dumping data for table `sensorInfo`
--
INSERT INTO `sensorInfo` (`id`, `sensorId`, `sensors`, `pinNo`, `sensorsType`) VALUES
(1, 1, 'Red LED', 7, 'Switch ON/OFF'),
(2, 2, 'Blue LED', 11, 'Switch ON/OFF'),
(3, 3, 'Green LED', 13, 'Switch ON/OFF');
答案 0 :(得分:0)
从路由器功能中删除导出呼叫 - 来自
exports.index = function(req, res) {
var connection = mysql.createConnection(conn);
var query ='SELECT sensorId, sensors, pinNo, sensorsType FROM sensorInfo';
connection.query(query, function(err, rows, fields) {
if(err) throw err;
res.render('index', { title: 'Sensor REST API', 'items': rows });
});
};
到
var connection = mysql.createConnection(conn);
var query ='SELECT sensorId, sensors, pinNo, sensorsType FROM sensorInfo';
connection.query(query, function(err, rows, fields) {
if (err) throw err;
res.render('index', { title: 'Sensor REST API', 'items': rows });
});