我有两个模型users.model.js和swing-trades.model.js我在这两个模型之间创建关系并使用npm-run-dev`运行feathersjs时,我收到了一个错误。
抛出新错误(
${this.name}.hasMany called with something that's not a subclass of Sequelize.Model
(;^错误:users.hasMany调用的对象不是的子类Sequelize.Mode
**users.model.js**
// See http://docs.sequelizejs.com/en/latest/docs/models-definition/
// for more of what you can do here.
const Sequelize = require('sequelize');
const DataTypes = Sequelize.DataTypes;
module.exports = function (app) {
const sequelizeClient = app.get('sequelizeClient');
const users = sequelizeClient.define('users', {
email: {
type: DataTypes.STRING,
allowNull: true,
unique: true
},
password: {
type: DataTypes.STRING,
allowNull: true
},
username: {
type: DataTypes.STRING,
allowNull: true,
},
firstName: {
type: DataTypes.STRING,
allowNull: true,
},
lastName: {
type: DataTypes.STRING,
allowNull: true,
}
},
{
timestamps: true, // Enable timestamps
createdAt: true, // Don't create createdAt
updatedAt: true, // Don't create updatedAt
// eslint-disable-next-line no-dupe-keys
updatedAt: 'updateTimestamp', // updatedAt should be called updateTimestamp
},
{
hooks: {
beforeCount(options) {
options.raw = true;
}
}
});
// eslint-disable-next-line no-unused-vars
users.associate = function (models) {
// Define associations here
// See http://docs.sequelizejs.com/en/latest/docs/associations/
const {swingTrades} = models;
users.hasMany(swingTrades, {as: 'swingTrades', foreignKey: 'userId'}); // will add userid to swingtrades model
};
return users;
};
swing-trades.model.js
// See http://docs.sequelizejs.com/en/latest/docs/models-definition/
// for more of what you can do here.
const Sequelize = require('sequelize');
const DataTypes = Sequelize.DataTypes;
module.exports = function (app) {
const sequelizeClient = app.get('sequelizeClient');
const swingTrades = sequelizeClient.define('swing_trades', {
company: {
type: DataTypes.STRING,
allowNull: true
}
},
{
timestamps: true, // Enable timestamps
createdAt: false, // Don't create createdAt
updatedAt: false, // Don't create updatedAt
// eslint-disable-next-line no-dupe-keys
updatedAt: 'updateTimestamp', // updatedAt should be called updateTimestamp
},
{
hooks: {
beforeCount(options) {
options.raw = true;
}
}
});
// eslint-disable-next-line no-unused-vars
swingTrades.associate = function (models) {
// Define associations here
// See http://docs.sequelizejs.com/en/latest/docs/associations/
const {users} = models;
swingTrades.belongsTo(users,{foreignKey: 'userId'});
};
return swingTrades;
};
我还没有找到任何解决方案。
这是因为模型名称和服务名称不匹配。这是swingTrades
**users.model.js**
// eslint-disable-next-line no-unused-vars
users.associate = function ({swingTrades) {
users.hasMany(swingTrades, {
as: 'swingTrades',
foreignKey: 'userId'
});
};
但在swing-trades.model.js
中,您有swing_trades
...
const swingTrades = sequelizeClient.define('swing_trades', {
company: {
type: DataTypes.STRING,
allowNull: true
}
},
...
解决方案是在创建新服务时避免空间和。用一个词来命名服务。