我正在使用consign从我的Node JS api上的模块进行自动加载,在这里我加载了所有的路由,模型和我的数据库连接函数,当我运行nodemom app
时,模块使连接加载但我无法连接到数据库,他把这个错误扔给我TypeError: Cannot read property 'db' of undefined
看看我的树文件:
我可以加载路线cuz
db.js(具有de数据库配置的文件)
var mysql = require('mysql');
var connMySQL = function () {
console.log("I've Started the connection")
return mysql.createConnection({
host: 'localhost',
user: 'root',
password: '',
database: 'agimplant'
});
}
module.exports = function () {
console.log("I've loaded this function")
return connMySQL;
}
Server.js
var express = require('express'),
bodyparser = require('body-parser'),
consign = require('consign'),
app = express();
consign()
.include('./src/routes')
.then('./src/config/db.js')
.then('./src/models')
.into(app);
app.use(bodyparser.urlencoded({ extended: true }));
app.listen(4000, function () {
console.log("Servidor ON");
});
module.exports = app;
连接调用Home.route.js
module.exports = function (app) {
app.get('/', (req, res) => {
console.log("Call the Connection Here")
var connection = app.config.db();
});
}
正如在src补丁的托管中所讨论的,将app.src.conf用于调用您的对象。总是让src用于方法访问,并使用来自debbug的console.log,这很棒