gpt4 book ai didi

node.js - 如何使用 Node.js 在 Sequelize 6 中实现实例方法

转载 作者:行者123 更新时间:2023-12-03 22:19:03 24 4
gpt4 key购买 nike

所以我在我的服务器应用程序中实现了一些更改 - 将数据库从 MongoDb 切换到 PostgreSQL(使用 Sequelize 6)并且我不得不更改我为 mongoose 创建的 Controller 函数以适应当前数据库但是有一个问题实现实例方法,但像往常一样,Sequelize 6 几乎没有在线解决方案对此有帮助。但现在有了。以下是您在阅读本文时可能遇到的一些问题和错误消息的代码示例。

调用实例方法的函数:userController.js(登录函数)

User.findByPk(req.body.id)
.then(user => {
if (!user) {
return res.status(401).send('Sorry!! You do not have an account with us.')
}

if (!user.validPassword(req.body.password)) {
return res.status(401).send('Invalid Password')
} else {
res.status(200).json({ user })
}
})
.catch(error => {
console.log(error)
res.status(500).send({
message: 'Some error occurred while logging in this User',
error: error.message
});
});

示例代码 1

用户.js

'use strict';
const { Model } = require('sequelize');

module.exports = (sequelize, DataTypes) => {
class User extends Model {
/**
* Helper method for defining associations.
* This method is not a part of Sequelize lifecycle.
* The `models/index` file will call this method automatically.
*/
static associate(models) {
// define association here
}
};

User.init({
username: {
type: DataTypes.STRING,
allowNull: false,
unique: true
},
password: {
type: DataTypes.STRING,
allowNull: false,
unique: true
},
password_confirmation: {
type: DataTypes.STRING,
allowNull: false,
unique: true
}
}, {
hooks: {
beforeCreate: (User) => {
const salt = bcrypt.genSaltSync();
User.password = bcrypt.hashSync(User.password, salt);
User.password_confirmation = User.password;
}
},
instanceMethods: {
validatePassword: (password) => {
return bcrypt.compareSync(password, this.password);
}
}
sequelize,
modelName: 'User',
});
return User;
};

响应(服务器 500 错误消息)

{
"message": "Some error occurred while logging in this User",
"error": "user.validPassword is not a function"
}

本例中的实例方法未被识别,函数 validPassword() 未运行,因此出现 500 服务器错误。让我们转到示例 2。

示例代码 2

用户.js

'use strict';
const { Model } = require('sequelize');

module.exports = (sequelize, DataTypes) => {
class User extends Model {
/**
* Helper method for defining associations.
* This method is not a part of Sequelize lifecycle.
* The `models/index` file will call this method automatically.
*/
static associate(models) {
// define association here
}
};

User.init({
username: {
type: DataTypes.STRING,
allowNull: false,
unique: true
},
password: {
type: DataTypes.STRING,
allowNull: false,
unique: true
},
password_confirmation: {
type: DataTypes.STRING,
allowNull: false,
unique: true
}
}, {
hooks: {
beforeCreate: (User) => {
const salt = bcrypt.genSaltSync();
User.password = bcrypt.hashSync(User.password, salt);
User.password_confirmation = User.password;
}
},
instanceMethods: {
validatePassword: (password) => {
return bcrypt.compareSync(password, this.password);
}
}
sequelize,
modelName: 'User',
});

User.prototype.validPassword = (password) => {
return bcrypt.compareSync(password, this.password);
};

return User;
};

响应(服务器 500 错误消息)

{
"message": "Some error occurred while logging in this User",
"error": "Illegal arguments: string, undefined"
}

在这种情况下,实例方法仍未被识别,因此函数 validPassword() 未运行,因为这里 的参数 this.password bcrypt.compareSync() 函数未定义(或已在模型扩展之外使用),因此出现 500 服务器错误。

现在是解决方案。

最佳答案

经过大约半天的搜索,出于某种原因,我发现 instanceMethods 功能 has been removed in Sequelize v4 .因此,获得此功能的唯一方法是通过以下方法之一:

  • 按照 Jeffrey Dabo 的建议在模型类上声明函数
  • 在Sequelize模型的prototype上添加函数

非常重要:如果您使用原型(prototype)方法,为了访问this对象,您需要使用函数语法声明函数而不是作为箭头函数,否则它将不起作用。

例子:

const User = sequelize.define('User', {...});

User.prototype.validatePassword = function (password) {
return bcrypt.compareSync(password, this.password);
}

关于node.js - 如何使用 Node.js 在 Sequelize 6 中实现实例方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66301876/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com