Node.js sequelize.define错误:没有方法';定义';在nodejs中

Node.js sequelize.define错误:没有方法';定义';在nodejs中,node.js,sequelize.js,Node.js,Sequelize.js,这是我的managedb.js,它管理所有数据库模型: var Sequelize = require('sequelize-postgres').sequelize var postgres = require('sequelize-postgres').postgres var db = new Sequelize('testdb', 'postgres', 'postgres', { dialect: 'postgres' }) var models = [ 'user' ]

这是我的managedb.js,它管理所有数据库模型:

var Sequelize = require('sequelize-postgres').sequelize
var postgres  = require('sequelize-postgres').postgres

 var db = new Sequelize('testdb', 'postgres', 'postgres', {
  dialect: 'postgres'
})


var models = [
'user'
];

models.forEach(function(model) {
module.exports[model] = db.import(__dirname + '/' + model);
});



exports.db = db;
这是我的user.js

var sequelize = require("sequelize");
var seq = require("./managedb");
var db = seq.db;

var Project = db.define('Project', {
  title: sequelize.STRING,
  description: sequelize.TEXT
});
在我的app.js中

var seq = require('./models/managedb');
seq.db.sync();
我得到的错误是:

var Project = sequelize.define('Project', {
                        ^
TypeError: Object function (database, username, password, options) {
    var urlParts
    options = options || {}

    if (arguments.length === 1 || (arguments.length === 2 && typeof username === 'object')) {
      options = username || {}
      urlParts = url.parse(arguments[0])
      database = urlParts.path.replace(/^\//,  '')
      dialect = urlParts.protocol
      options.dialect = urlParts.protocol.replace(/:$/, '')
      options.host = urlParts.hostname

      if (urlParts.port) {
        options.port = urlParts.port
      }

      if (urlParts.auth) {
        username = urlParts.auth.split(':')[0]
        password = urlParts.auth.split(':')[1]
      }
    }

    this.options = Utils._.extend({
      dialect: 'mysql',
      host: 'localhost',
      port: 3306,
      protocol: 'tcp',
      define: {},
      query: {},
      sync: {},
      logging: console.log,
      omitNull: false,
      queue: true,
      native: false,
      replication: false,
      pool: {},
      quoteIdentifiers: true
    }, options || {})

    if (this.options.logging === true) {
      console.log('DEPRECATION WARNING: The logging-option should be either a function or false. Default: console.log')
      this.options.logging = console.log
    }

    this.config = {
      database: database,
      username: username,
      password: (( (["", null, false].indexOf(password) > -1) || (typeof password == 'undefined')) ? null : password),
      host    : this.options.host,
      port    : this.options.port,
      pool    : this.options.pool,
      protocol: this.options.protocol,
      queue   : this.options.queue,
      native  : this.options.native,
      replication: this.options.replication,
      maxConcurrentQueries: this.options.maxConcurrentQueries
    }

    var ConnectorManager = require("./dialects/" + this.options.dialect + "/connector-manager")

    this.daoFactoryManager = new DAOFactoryManager(this)
    this.connectorManager  = new ConnectorManager(this, this.config)

    this.importCache = {}
  } has no method 'define'

这是因为您必须实例化Sequelize

var Sequelize = require("sequelize");

var sequelize = new Sequelize('database', 'username');

var Project = sequelize.define('Project', {
  title: sequelize.STRING,
  description: sequelize.TEXT
});

我必须在每个模型中实例化sequelize吗?我可以从管理数据库导入它,对吗?是的,通常您应该为您的模型使用相同的实例。编辑:您可以module.exports您的实例并使用它在其他文件中定义您的模型。错误仍然存在。我编辑了问题以显示代码中的更改。我是否导出/导入错误?我当前得到的错误是:TypeError:无法调用未定义的方法“define”,如果使用module.exports.db=db而不是exports.db=db,它会工作吗?