最新消息: 电脑我帮您提供丰富的电脑知识,编程学习,软件下载,win7系统下载。

在尝试使用consign将mysql与节点js连接时获取未定义

IT培训 admin 8浏览 0评论

在尝试使用consign将mysql与节点js连接时获取未定义

我正在使用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,这很棒

在尝试使用consign将mysql与节点js连接时获取未定义

我正在使用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,这很棒

发布评论

评论列表 (0)

  1. 暂无评论