node.js - 尝试在打字稿中使用猫鼬和 bcrypt 比较密码
问题描述
我在第 40 行收到以下错误:
bcrypt.compare(candidatePassword, this.password, function (err, isMatch) {
~~~~~~~~~~~~~
“函数”类型的参数不可分配给“字符串”类型的参数。ts(2345)
根据 IUser 它应该是字符串,我不知道为什么 ts 意味着它是一个函数。
完整的文件:
import { Schema, Document } from 'mongoose';
import bcrypt from 'bcrypt';
const salt: number = 12;
const UserSchema: Schema<IUser> = new Schema({
email: {
type: String,
required: true,
unique: true,
},
name: {
type: String,
required: true,
minlength: 3,
maxlength: 32,
},
password: {
type: String,
required: true,
},
});
// * Hash the password befor it is beeing saved to the database
UserSchema.pre('save', function (next: (err: Error | null) => void) {
// * Make sure you don't hash the hash
if (!this.isModified('password')) {
return next(null);
}
bcrypt.hash(this.password, salt, (err: Error, hash: String) => {
if (err) return next(err);
this.password = hash;
});
});
UserSchema.methods.comparePasswords = function (
candidatePassword: String,
next: (err: Error | null, same: Boolean | null) => void,
) {
bcrypt.compare(candidatePassword, this.password, function (err, isMatch) {
if (err) {
return next(err, null);
}
next(null, isMatch);
});
};
export interface IUser extends Document {
email: String;
name: String;
password: String;
}
解决方案
TypeScript 的基本类型有:string
、、boolean
等number
。请参阅基本类型。但是您正在使用 JavaScript 数据类型的构造函数(String
等Boolean
)。请参阅JavaScript 数据类型和数据结构
正确的方法:
import { Schema, Document } from 'mongoose';
import bcrypt from 'bcrypt';
const salt: number = 12;
const UserSchema: Schema<IUser> = new Schema({
email: {
type: String,
required: true,
unique: true,
},
name: {
type: String,
required: true,
minlength: 3,
maxlength: 32,
},
password: {
type: String,
required: true,
},
});
// * Hash the password befor it is beeing saved to the database
UserSchema.pre('save', function (this: IUser, next: (err?: Error | undefined) => void) {
// * Make sure you don't hash the hash
if (!this.isModified('password')) {
return next();
}
bcrypt.hash(this.password, salt, (err: Error, hash: string) => {
if (err) return next(err);
this.password = hash;
});
});
UserSchema.methods.comparePasswords = function (
candidatePassword: string,
next: (err: Error | null, same: boolean | null) => void,
) {
bcrypt.compare(candidatePassword, this.password, function (err, isMatch) {
if (err) {
return next(err, null);
}
next(null, isMatch);
});
};
export interface IUser extends Document {
email: string;
name: string;
password: string;
comparePasswords(candidatePassword: string, next: (err: Error | null, same: boolean | null) => void): void;
}
包版本:
"mongoose": "^5.7.11"
"@types/mongoose": "^5.5.32"
"typescript": "^3.7.2"
推荐阅读
- ruby-on-rails - 如何检查 Rails 的 ActiveStorage 中的附件是否是新的?
- java - 我可以为不同活动中的两个不同列表视图使用相同的适配器吗?
- angular - 如何在视图中最好地订阅 NGRX 状态变化?
- mysql - 如何修复 MySql 工作台中创建模式的“错误解析 DDL”?
- java - 如何在 Validator 类中使用注解?
- apache - 如何使用多个 url 变量?
- intellij-idea - 如何修复错误:RuntimeException Unmatched delimiter:)
- qemu - 在 Gem5 全系统模拟 x86 上使用 Gentoo linux 编译和运行 C 程序
- python - 尝试在循环下打印多个变量
- regex - RegEx 用于验证具有单字母域的电子邮件