这是我管理所有数据库模型的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'
答案 0 :(得分:5)
这是因为你必须实例化Sequelize。
var Sequelize = require("sequelize");
var sequelize = new Sequelize('database', 'username');
var Project = sequelize.define('Project', {
title: sequelize.STRING,
description: sequelize.TEXT
});