2013-06-07 22 views
5

To jest moje managedb.js który zarządza wszystkimi modeli bazy danych:błąd sequelize.define: nie ma metody 'określenie' w nodejs

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; 

To jest mój 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 
}); 

W moi app.js

var seq = require('./models/managedb'); 
seq.db.sync(); 

błąd otrzymuję to:

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' 

Odpowiedz

5

Dzieje się tak, ponieważ musisz utworzyć instancję Sequelize.

var Sequelize = require("sequelize"); 

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

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

Czy muszę więc utworzyć instancję sequelize w każdym modelu? Mogę po prostu zaimportować go z bazy do zarządzania, prawda? – Hick

+0

Tak, ogólnie powinieneś używać tej samej instancji dla swoich modeli. Edytuj: możesz moduł wyeksportować swoją instancję i użyć jej do zdefiniowania modeli w innych plikach. –

+0

Błąd nadal występuje. Zmieniłem pytanie, aby pokazać zmianę kodu. Czy eksportowałem/importowałem źle? – Hick

Powiązane problemy