TypeError:找不到数据库对象

时间:2014-12-15 19:24:57

标签: javascript node.js express

我正在使用"express": "3.2.6",nodeJS v0.10.25

运行我的app.js时,我得到了:

TypeError: Object #<IncomingMessage> has no method 'getConnection'

我的app.js

/**
 * Module dependencies.
 */

var express = require('express')
, routes = require('./routes')
, user = require('./routes/user')
, http = require('http')
, path = require('path');

var app = express();

//database connection
var connection  = require('express-myconnection'); 
var mysql = require('mysql');
//all environments
app.set('port', process.env.PORT || 3000);
app.set('views', __dirname + '/views');
app.set('view engine', 'jade');
app.use(express.favicon());
app.use(express.logger('dev'));
app.use(express.bodyParser());
app.use(express.methodOverride());
app.use(app.router);
app.use(express.static(path.join(__dirname, 'public')));

//development only
if ('development' == app.get('env')) {
    app.use(express.errorHandler());
}


/*------------------------------------------
connection peer, register as middleware
type koneksi : single,pool and request 
-------------------------------------------*/
app.use(
        connection(mysql,{
            host: 'localhost',
            user: 'root',
            password : '',
            port : 3306, //port mysql
            database:'db-test'
        },'pool')
);

//routes
//app.get('/', routes.index);
app.get('/', routes.list);
app.get('/users', user.list);

http.createServer(app).listen(app.get('port'), function(){
    console.log('Express server listening on port ' + app.get('port'));
});

我的index.js文件看起来像这样:

/*
 * GET home page.
 */

//exports.index = function(req, res){
//  res.render('index', { title: 'Express' });
//};

/**
 * returns all trade objects
 */
exports.list = function(req, res){
    req.getConnection(function(err,connection){

        connection.query('SELECT * FROM trades',function(err,rows)     {

            if(err)
                console.log("Error Selecting : %s ",err );

            res.render('customers',{page_title:"Customers - Node.js",data:rows});

        });

    });
};  

我收到以下异常:

Express server listening on port 3000
TypeError: Object #<IncomingMessage> has no method 'getConnection'
    at exports.list (C:\Users\nodeWorkspace\Test\routes\index.js:14:6)
    at callbacks (C:\Users\nodeWorkspace\Test\node_modules\express\lib\router\index.js:161:37)
    at param (C:\Users\nodeWorkspace\Test\node_modules\express\lib\router\index.js:135:11)
    at pass (C:\Users\nodeWorkspace\Test\node_modules\express\lib\router\index.js:142:5)
    at Router._dispatch (C:\Users\nodeWorkspace\Test\node_modules\express\lib\router\index.js:170:5)
    at Object.router (C:\Users\nodeWorkspace\Test\node_modules\express\lib\router\index.js:33:10)
    at next (C:\Users\nodeWorkspace\Test\node_modules\express\node_modules\connect\lib\proto.js:190:15)
    at Object.methodOverride [as handle] (C:\Users\nodeWorkspace\Test\node_modules\express\node_modules\connect\lib\middleware\methodOverride.js:37:5)
    at next (C:\Users\nodeWorkspace\Test\node_modules\express\node_modules\connect\lib\proto.js:190:15)
    at multipart (C:\Users\nodeWorkspace\Test\node_modules\express\node_modules\connect\lib\middleware\multipart.js:64:37)
[90mGET / [31m500 [90m59ms

任何建议我做错了什么?

感谢您的回答!

1 个答案:

答案 0 :(得分:1)

看起来您使用了类似这样的示例:http://teknosains.com/i/simple-crud-nodejs-mysql但更改了代码序列 在声明的app.use(app.router);下移动app.get/app.post 您还可以使用其他mysql集成的情况:https://www.npmjs.com/package/express-myconnection