使用mongoose同步多个schmeas

使用mongoose同步多个schmeas,mongoose,Mongoose,我有一个目录,其中定义了许多Mongoose模式。 例如users.js: var mongoose = require('mongoose') , Schema = mongoose.Schema , ObjectId = Schema.ObjectId; var users = new mongoose.Schema({ id: String, // mongoose should create it automatically username:String, password:{ t

我有一个目录,其中定义了许多Mongoose模式。 例如users.js:

var mongoose = require('mongoose')
, Schema = mongoose.Schema
, ObjectId = Schema.ObjectId;

var users = new mongoose.Schema({
id: String,  // mongoose should create it automatically
username:String,
password:{ type: String, required: true, bcrypt: true },
})

users.plugin(require('mongoose-bcrypt'));

module.exports = mongoose.model('Users', users);
我想把所有这些schemad添加到DB中。因此,我将使用下一个文件db.js:

import fs from "fs";
import path from "path";
var mongoose = require('mongoose')

let db = null;

module.exports = app => {
  const config = app.libs.config;
  if (!db) {

      db = mongoose.createConnection(config.host, config.database);
    const dir = path.join(__dirname, "mongoModels");
    fs.readdirSync(dir).forEach(file => {
      const model = path.join(dir, file);

      db.models[model.name] = require(model);
    });

  }

  return db;
};
但当我稍后在代码中尝试使用这些模型时,我发现它没有定义

我认为问题是-可能是因为模型没有同步到db。 在切换到mongo db之前,我在这个应用程序中使用了sequelize, 在那里我有以下几句话:

  //app.db.sequelize.sync().done(() => {
    app.listen(app.get("port"), () => {
      console.log(`Port ${app.get("port")}`);
      });
//  });
如您所见,我对sync方法进行了注释,该方法将所有定义的模式同步到数据库。有没有办法在猫鼬中执行相同的命令? 或者任何人有另一个想法,问题出在哪里? 提前谢谢