简体   繁体   English

TypeError:使用Sequelize在NodeJs中定义模型时,object不是函数

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

This is my managedb.js 这是我的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: 这是我在库中的models.js:

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? 我不能在单个js文件中定义多个模型吗?

Sequelize.import expects a function to be returned from your model file(s) . Sequelize.import期望从模型文件返回一个函数 I have each model in a separate file. 我将每个模型都放在一个单独的文件中。 For example, link.js could be something like this: 例如,link.js可能是这样的:

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: 然后在您的managedb.js文件中,您可以遍历文件:

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

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何导出和使用 sequelize 模型 nodejs - How to export and using sequelize models nodejs 使用Jasmine Sequelize TypeError进行测试:_models2.default.count不是函数 - Testing with Jasmine Sequelize TypeError: _models2.default.count is not a function TypeError:object不是函数+ nodeJS - TypeError: object is not a function+nodeJS UnhandledPromiseRejectionWarning: TypeError: user.destroy is not a function (NodeJS- Sequelize) - UnhandledPromiseRejectionWarning: TypeError: user.destroy is not a function (NodeJS- Sequelize) Model sequelize nodejs TypeError: (intermediate value).init is not a function - Model sequelize nodejs TypeError: (intermediate value).init is not a function 使用Jquery和Object属性函数时出现TypeError - TypeError when using Jquery and Object property function TypeError:使用csv模块时object不是函数 - TypeError: object is not a function when using csv module reactjs TypeError:使用钩子时Object(…)不是函数 - reactjs TypeError: Object(…) is not a function when using hooks TypeError: Object(...) 在使用 useHistory() 钩子时不是函数 - TypeError: Object(...) is not a function when using useHistory() hooks TypeError:使用nodejs,selenium和mocha时,test.describe不是函数 - TypeError: test.describe is not a function when using nodejs, selenium and mocha
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM