Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

sequelize.define error: has no method 'define' in nodejs

This is my managedb.js which manages all the database models:

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;

This is my 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
});

In my app.js

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

Error I get is this:

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'
like image 675
Hick Avatar asked Jun 07 '13 19:06

Hick


1 Answers

This is because you have to instantiate Sequelize.

var Sequelize = require("sequelize");

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

var Project = sequelize.define('Project', {
  title: sequelize.STRING,
  description: sequelize.TEXT
});
like image 69
Jean-Philippe Leclerc Avatar answered Nov 15 '22 07:11

Jean-Philippe Leclerc