迁移到mongodb地图集db.once('open',()=> {

时间:2019-05-01 01:15:47

标签: node.js database mongodb mongoose mongodb-atlas

将我的数据库从Compose迁移到Mongodb Altas之后。我将MONGO_URI更改为mongodb Altas,db.once('open',无法正常工作

const mongoose = require('mongoose'),
  fs = require('fs'),
  Promise = require('bluebird'),
  modelsPath = './models',
  glob = Promise.promisify(require('glob')),
  _ = require('lodash');

const models = () => glob(`${modelsPath}/**/*.js`);

module.exports = {
  init: () => {
    mongoose.connect(process.env.MONGO_URI);

    let db = mongoose.connection;

    models()
      .then(m => {
        m.forEach(model => require(`../${model}`));

        db.on('error', console.error.bind(console, 'Error connecting to mongodb: '));
        db.once('open', console.log.bind(console, 'Database connected.'));

        mongoose.Promise = require('bluebird');
      });
  }
};

0 个答案:

没有答案