Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/mongodb/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Node.js 节点将mongoDB连接对象传递给控制器_Node.js_Mongodb_Ecmascript 6_Controller_Router - Fatal编程技术网

Node.js 节点将mongoDB连接对象传递给控制器

Node.js 节点将mongoDB连接对象传递给控制器,node.js,mongodb,ecmascript-6,controller,router,Node.js,Mongodb,Ecmascript 6,Controller,Router,我正在尝试使用nodejs和mongodb构建经典的路由器控制器app dbconnection结构。 我正在努力解决的问题如下: 以下类别: class Database { constructor(dbase) { this.dbase = dbase; } makeConnect(uri, options) { new MongoClient.connect(uri, options, (err, db) => { if (err) {

我正在尝试使用nodejs和mongodb构建经典的路由器控制器app dbconnection结构。 我正在努力解决的问题如下:

以下类别:

class Database {

constructor(dbase) {
    this.dbase = dbase;
}

makeConnect(uri, options) {
    new MongoClient.connect(uri, options, (err, db) => {
        if (err) {
            return console.log(err)
        }
        this.db = db.db(this.dbase);
    });
    return this.db;
}
}

export default Database;
和路由器下方:

const router = express.Router();
const mongoUtils = new MongoUtils();
mongoUtils.setConfigs('root', 'root');
const configs = mongoUtils.getConfigs();

const myController = new CompaniesController(new Database('sample_training').makeConnect(configs.uri, configs.options));

router.get('/api/v1/companies', myController.getAllCompanies);

export default router;
我获取
无法读取未定义的属性“db”


我缺少什么?

公司控制器中实例化db对象

class CompaniesController { 
      db:any;

我没有为此任务使用typescript,请尝试更改为const或let,而不是使用此变量
class CompaniesController { 
      db:any;