【发布时间】:2022-01-21 02:06:56
【问题描述】:
我创建了一个用户模型:
class UserModel extends Model {
static table = 'users'
static timestamps = true
static fields = {
id: { primaryKey: true, autoIncrement: true},
firstName: DataTypes.STRING,
lastName: DataTypes.STRING,
username: DataTypes.STRING,
email: DataTypes.STRING,
password: DataTypes.STRING,
birthday: DataTypes.DATE,
phoneNumber: DataTypes.INTEGER,
}
}
当我将现有用户密码与新密码进行比较时:
async signin(user: Pick<User, "username" | "password">){
const { username, password } = user;
const existentUser = await UserModel.where('username', username).first()
if (!existentUser) throw new CustomErrorHandler(Status.NotFound, "User does not exist")
const isPasswordCorrect = await bcrypt.compare(password, existentUser.password); // Argument of type 'Function | FieldValue' // is not assignable to parameter of type 'string'.
// Type 'null' is not assignable to type 'string'.
}
我收到了这个 ts 错误:
Argument of type 'Function | FieldValue' is not assignable to parameter of type 'string'.
Type 'null' is not assignable to type 'string'.
我可以通过使用强制类型来修复它:
const isPasswordCorrect = await bcrypt.compare(password, <string>existentUser.password);
但我正在寻找另一种解决方案。是否有另一种方法可以将 first() 返回的模型转换为 User interface 或其他方式?
【问题讨论】: