Sequelize:TypeError:User.hasMany不是函数

时间:2017-11-28 17:57:05

标签: javascript node.js postgresql express sequelize.js

我有这种奇怪的行为。 我有一个用户模型和一个客户端模型。用户有很多客户端。 我正在使用此文档:http://docs.sequelizejs.com/manual/tutorial/associations.html

当我运行服务器时,Sequelize抛出 TypeError:User.hasMany不是函数

节点版本: 8.9.1
方言: postgres
数据库版本: Postgres 10
Sequelize版本: 4.22.15

以下文件都在同一个文件夹中

user.js模型

const Sequelize = require('sequelize');
const db = require('../index.js'); //This is an instance of new Sequelize(...)

const tableName = 'users';

const User = db.define('user', {
  firstName: {
    type: Sequelize.STRING(50),
    allowNull: false,
    validate: {
      notEmpty: true
    }
  },
  lastName: {
    type: Sequelize.STRING(50),
    allowNull: false,
    validate: {
      notEmpty: true
    }
  },
  username: { //Username will be the email
    type: Sequelize.STRING(80),
    allowNull: false,
    unique: true,
    validate: {
      isEmail: true
    }
  },
  password: {
    type: Sequelize.STRING,
    allowNull: false,
    validate: {
      notEmpty: true
    }
  },
  isAdmin: {
    type: Sequelize.BOOLEAN,
    allowNull: false,
    defaultValue: false
  },
  isActive: {
    type: Sequelize.BOOLEAN,
    allowNull: false,
    defaultValue: false
  }

}, { tableName });

module.exports = User;

client.js模型

'use strict'

const Sequelize = require('sequelize');
const db = require('../index.js');

const instanceMethods = {
  toJSON() {
    const values = Object.assign({}, this.get());

    return values;
  },
};

const Client = db.define('clients', {
  ibid: {
    type: Sequelize.BIGINT,
    allowNull: true,
    defaultValue: null
  },
  firstName: {
    type: Sequelize.STRING(50),
    allowNull: false,
    validate: {
      notEmpty: true
    }
  },
  lastName: {
    type: Sequelize.STRING(50),
    allowNull: false,
    validate: {
      notEmpty: true
    }
  },
  agreementSigned: {
    type: Sequelize.BOOLEAN,
    allowNull: false,
    defaultValue: false
  },
  totalBalance: {
    type: Sequelize.DECIMAL(11,2),
    allowNull: true,
    defaultValue: null
  },
  currentAllocation: {
    type: Sequelize.STRING,
    allowNull: true,
    defaultValue: null
  }

}, { instanceMethods });

module.exports = Client;

index.js模型

'use strict';

const User = require('./user')
const Client = require('./client');

User.hasMany(Client);
Client.belongsTo(User);

module.exports = {User, Client};

1 个答案:

答案 0 :(得分:1)

在index.js中, 你还没有导出你初始化的新的sequelize实例,因此它不能用于client.js和user.js! define函数适用于您创建的sequelize实例对象!

此外,项目中存在循环依赖关系,可能会产生问题!

我尝试了您的代码并更改了结构,现在可以使用了!可能还有其他结构可能,但这个对我有用!

1)不要在index.js中创建一个新的sequelize实例,而是创建另一个文件,作为sequelize实例的初始化!

sequelize_index.js(我使用此文件创建基本实例,然后在客户端和用户模型中使用此基本实例)

const Sequelize = require('sequelize');
const dbconfig=require('./dbconfig.json');

const sequelize = new Sequelize('postgres://' + dbconfig.USER + ":" +     dbconfig.PASSWORD + "@" + dbconfig.HOST + ":5432/" + dbconfig.DB, {
host: dbconfig.HOST,
dialect: dbconfig.DIALECT,
pool: {
    min: 0,
    max: 5,
    idle: 1000
     }
});

 module.exports={sequelize};

2)cli​​ent.js看起来像这样!

'use strict'
const Sequelize = require('sequelize');

const sequelize=require('./sequelize_index').sequelize;
const db = require('./index.js');

const instanceMethods = {
    toJSON() {
        const values = Object.assign({}, this.get());

        return values;
    },
};

const Client = sequelize.define('clients', {
    ibid: {
        type: Sequelize.BIGINT,
        allowNull: true,
        defaultValue: null
    },
    firstName: {
        type: Sequelize.STRING(50),
        allowNull: false,
        validate: {
            notEmpty: true
        }
    },
    lastName: {
        type: Sequelize.STRING(50),
        allowNull: false,
        validate: {
            notEmpty: true
        }
    },
    agreementSigned: {
        type: Sequelize.BOOLEAN,
        allowNull: false,
        defaultValue: false
    },
    totalBalance: {
        type: Sequelize.DECIMAL(11,2),
        allowNull: true,
        defaultValue: null
    },
    currentAllocation: {
        type: Sequelize.STRING,
        allowNull: true,
        defaultValue: null
    }

}, { instanceMethods });

module.exports = Client;

3)User.js

const Sequelize = require('sequelize');
const sequelize=require('./sequelize_index').sequelize;
const db = require('./index.js'); //This is an instance of new Sequelize(...)

const tableName = 'users';

const User = sequelize.define('user', {
    firstName: {
        type: Sequelize.STRING(50),
        allowNull: false,
        validate: {
            notEmpty: true
        }
    },
    lastName: {
        type: Sequelize.STRING(50),
        allowNull: false,
        validate: {
            notEmpty: true
        }
    },
    username: { //Username will be the email
        type: Sequelize.STRING(80),
        allowNull: false,
        unique: true,
        validate: {
            isEmail: true
        }
    },
    password: {
        type: Sequelize.STRING,
        allowNull: false,
        validate: {
            notEmpty: true
        }
    },
    isAdmin: {
        type: Sequelize.BOOLEAN,
        allowNull: false,
        defaultValue: false
    },
    isActive: {
        type: Sequelize.BOOLEAN,
        allowNull: false,
        defaultValue: false
    }

}, { tableName });

module.exports = User;

4)index.js

'use strict';
const sequelize = require('./sequelize_index').sequelize;
const User = require('./user')
const Client = require('./client');


User.hasMany(Client);
Client.belongsTo(User);

sequelize.sync({force: false}).then(function () {
    console.log("Database Configured");
});
module.exports = {User, Client};

运行index.js后,将创建数据库!