为什么express-myconnection返回" undefined"连接参数?

时间:2017-10-16 01:32:57

标签: javascript mysql node.js express

我有一个基本的Node JS应用程序(因为我只是在学习)。我使用express,express-generator,express-myconnection和mysql。

该问题与查询数据库连接本身有关。

该应用程序使用MVC结构设计。

编辑:开始时,这是我的" app.js":

            var express = require('express');
            var path = require('path');
            var favicon = require('serve-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();

            var connection = require('express-myconnection');
            var mysql = require('mysql');

            app.use(

                connection(mysql,{

                    "host":"localhost",
                    "user":"root",
                    "password":"root",
                    "port":3306,
                    "database":"fruits"

                },'request')

            );

            // view engine setup
            app.set('views', path.join(__dirname, 'views'));
            app.set('view engine', 'pug');

            // uncomment after placing your favicon in /public
            //app.use(favicon(path.join(__dirname, 'public', 'favicon.ico')));
            app.use(logger('dev'));
            app.use(bodyParser.json());
            app.use(bodyParser.urlencoded({ extended: false }));
            app.use(cookieParser());
            app.use(express.static(path.join(__dirname, 'public')));

            app.use('/', index);

            // 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 handler
            app.use(function(err, req, res, next) {
              // set locals, only providing error in development
              res.locals.message = err.message;
              res.locals.error = req.app.get('env') === 'development' ? err : {};

              // render the error page
              res.status(err.status || 500);
              res.render('error');
            });

            module.exports = app;

我有一个模型文件" fruits.js":

            var fruits = function(data, req){
                this.data = data;
                this.req = req;
            }

            fruits.prototype.data = {};

            fruits.prototype.getAll = function(callback){

                this.req.getConnection(function(err, connection){

                    console.log(connection);

                    //var q = connection.query("SELECT * FROM `fruits`", function(err, rows){

                        //callback(rows);

                    //});

                });


            };

            module.exports = fruits;

然后我还有一个控制器文件(index.js):

            var express = require('express');
            var router = express.Router();

            var fruits = require('../models/fruits.js');

            /* GET home page. */
            router.get('/', function(req, res, next) {
              res.render('index', { title: 'Express' });
            });

            /*GET fruits page */
            router.get('/fruits',function(req, res, next){

                var f = new fruits({}, req);

                f.getAll(function(fruitsObj){

                    console.log(fruitsObj);

                    res.render('fruits',{
                        "title":"Fruits!",
                        "fruits":fruitsObj
                    });

                });
            });

            module.exports = router;

当水果路线被导航到时,会发生什么,我无法查询数据库。它说"连接"来自" this.req.getConnection"是"未定义"。

有什么理由可以检索数据库连接并根据这两个文件的内容进行查询?我很肯定我安装了所有的软件包。我甚至再次为他们跑了npm install以确保。

提前感谢您的帮助。

0 个答案:

没有答案