本文介绍了类型错误:使用 Sequelize 在 NodeJs 中定义模型时,对象不是函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的 managedb.js

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;

这是我在库中的models.js:

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;

我哪里出错了?我不能在一个 js 文件中定义多个模型吗?

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

推荐答案

Sequelize.import 需要一个函数从您的模型文件返回.我将每个模型放在一个单独的文件中.例如,link.js 可能是这样的:

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: " "
        },

        ...

    });
}

然后在您的 managedb.js 文件中,您可以遍历文件:

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);
});

这篇关于类型错误:使用 Sequelize 在 NodeJs 中定义模型时,对象不是函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-18 11:10