尝试使用 consign 将 mysql 与节点 js 连接时未定义
Getting undefined on try to connect mysql with node js using consign
我正在使用 consign 从我的 Node JS api 上的模块自动加载,在此我加载我所有的路由、模型和我的数据库连接函数,当我 运行 nodemom app
建立连接的模块已加载,但我无法连接到数据库,他向我抛出此错误 TypeError: Cannot read property 'db' of undefined
看看我的树文件:
我可以加载路线,因为
db.js(具有数据库配置的文件)
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 用于方法访问并从调试中使用 console.log 它很棒
我正在使用 consign 从我的 Node JS api 上的模块自动加载,在此我加载我所有的路由、模型和我的数据库连接函数,当我 运行 nodemom app
建立连接的模块已加载,但我无法连接到数据库,他向我抛出此错误 TypeError: Cannot read property 'db' of undefined
看看我的树文件:
我可以加载路线,因为
db.js(具有数据库配置的文件)
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 用于方法访问并从调试中使用 console.log 它很棒