简体   繁体   中英

TypeError: object is not a function when defining models in NodeJs using Sequelize

This is my managedb.js

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

 var db = new Sequelize('tesf3', 'postgres', 'postgres', {
  dialect: 'postgres',
  port: '5432',
  omitNull: true
});


module.exports.db = db;

var Link = db.import(__dirname + '/lib/link/models').Link;
var LinkUser = db.import(__dirname + '/lib/link/models').LinkUser;

module.exports.Link = Link;
module.exports.LinkUser = LinkUser;

This is my models.js in a library:

var sequelize = require('../../managedb').db;
var DataTypes = require('sequelize-postgres').sequelize;

var Link = sequelize.define('Link', {
    url: {
      type: DataTypes.STRING,
      validate:{
        isUrl: true,
        notEmpty: true,
        notNull: true
      }
    },
    context: {
      type: DataTypes.STRING,
      defaultValue: " "
    },
    previewImage: {
      type: DataTypes.STRING
    },
    source:{
      type: DataTypes.STRING
    },
    shortUrl:{
      type: DataTypes.STRING
    },
    viewed:{
      type: DataTypes.STRING
    },
    image:{
      type: DataTypes.STRING
    },
    id: {
      type: DataTypes.INTEGER,
      primaryKey: true,
      autoIncrement: true
    }
  },
    {
    instanceMethods: {
      countTasks: function() {
        // how to implement this method ?
      }
    }
  });


var LinkUser = sequelize.define('LinkUser', {
    linkId: {
      type: DataTypes.INTEGER
    },
    userId: {
      type: DataTypes.INTEGER
    },
    id: {
      type: DataTypes.INTEGER,
      primaryKey: true,
      autoIncrement: true
    }
  },
    {
    instanceMethods: {
      countTasks: function() {
        // how to implement this method ?
      }
    }
  });


module.exports.Link = Link;
module.exports.LinkUser = LinkUser;

Where am I going wrong? Can't I define more than one model in a single js file?

Sequelize.import expects a function to be returned from your model file(s) . I have each model in a separate file. For example, link.js could be something like this:

module.exports = function(sequelize, DataTypes) {
    return sequelize.define('Link', {
        url: {
          type: DataTypes.STRING,
          validate:{
            isUrl: true,
            notEmpty: true,
            notNull: true
          }
        },
        context: {
          type: DataTypes.STRING,
          defaultValue: " "
        },

        ...

    });
}

Then in your managedb.js file, you can loop through the files:

// load models
var models = [
 'Link',
 'LinkUser'
];

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM