编辑:我的示例中未包含代码行,该行已添加并带有注释。
我正在为新项目设置服务器,并尝试自v5以来首次使用Sequelize。在运行服务器后,将引发此错误。
/Users/me/dev/projects/my-project/node_modules/sequelize/lib/model.js:730
options = Utils.merge(_.cloneDeep(globalOptions.define), options);
^
TypeError: Cannot read property 'define' of undefined
at Function.init (/Users/me/dev/projects/my-project/node_modules/sequelize/lib/model.js:730:53)
at Object.<anonymous> (/Users/me/dev/projects/my-project/src/server/db/models/user.js:16:6)
at Module._compile (internal/modules/cjs/loader.js:736:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:747:10)
...
在调用模型定义初始化期间发生错误:
const sequelize = require("../_db");
...
console.log('this runs');
class User extends Sequelize.Model {} // edit: this line was here
User.init(
{
avatar: {
default: {
type: Sequelize.URL,
value: defaultAvatar
},
type: Sequelize.STRING
},
email: {
allowNull: false,
type: Sequelize.STRING,
unique: true
},
id: {
defaultValue: Sequelize.UUIDV1,
primaryKey: true,
type: Sequelize.UUID
},
name: {
type: Sequelize.STRING
},
password: {
allowNull: false,
get() {
return () => this.getDataValue("password");
},
type: Sequelize.STRING
},
salt: {
get() {
return () => this.getDataValue("salt");
},
type: Sequelize.STRING
}
},
{ sequelize, modelName: "user" }
);
console.log('this doesn't);
对我来说这很神秘,我不确定什么地方出了错。
答案 0 :(得分:1)
您需要包括用户的类定义。 在您提供的代码中看不到它。 这可以解释为什么错误说明它来自“未定义”
即
const Model = Sequelize.Model;
class User extends Model {}
您可以这样做:
class User extends Sequelize.Model {}
请参阅此处: https://sequelize.org/master/manual/models-definition.html
此外,看起来您正在使用Sequelize的默认模型加载器,您需要在
内定义该模型。module.exports = (sequelize, DataTypes) => {
const User = sequelize.define("User", {
});
return User
};