我正在使用nodejs和typescript构建一个用户模型。在我的mongoose模型中,我试图向Schema的email字段添加一个match属性,但我总是收到以下typescript错误。
Argument of type '{ firstName: { type: any; required: [true, string]; }; lastName: { type: any; required: [true, string]; }; email: { type: any; required: [true, string]; unique: true; match: (string | RegExp)[]; }; password: { ...; }; role: { ...; }; resetPasswordToken: any; resetPasswordExpire: DateConstructor; }' is not assignable to parameter of type 'SchemaDefinition'.
Property 'email' is incompatible with index signature.
Type '{ type: any; required: [true, string]; unique: true; match: (string | RegExp)[]; }' is not assignable to type 'string | Function | Function[] | Schema<Document<any>, Model<Document<any>>> | SchemaDefinition | SchemaTypeOptions<any> | Schema<Document<any>, Model<Document<any>>>[] | SchemaTypeOptions<...>[] | SchemaDefinition[]'.
Type '{ type: any; required: [true, string]; unique: true; match: (string | RegExp)[]; }' is not assignable to type 'string'.
这是模型文件:
使用者.ts
import mongoose, { Model, Schema } from 'mongoose';
import crypto from 'crypto';
import bcrypt from 'bcryptjs';
import jwt from 'jsonwebtoken';
import User from '../interfaces/User';
const userSchema: Schema = new mongoose.Schema({
firstName: {
type: String,
required: [true, 'Please enter a first name']
},
lastName: {
type: String,
required: [true, 'Please enter a last name']
},
email: {
type: String,
required: [true, 'Please enter an email'],
unique: true,
match: [
/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/,
'Please add a valid email'
]
},
password: {
type: String,
required: [true, 'Please add a password'],
minlength: 6,
select: false
},
role: {
type: String,
enum: ['admin', 'customer'],
default: 'customer'
},
resetPasswordToken: String,
resetPasswordExpire: Date
}, {
timestamps: true
});
userSchema.pre<User>('save', async function(next) {
if (!this.isModified('password')) {
next();
}
const salt = await bcrypt.genSalt(10);
this.password = await bcrypt.hash(this.password, salt);
});
userSchema.methods.getSignedJwtToken = function(next: any) {
// @ts-ignore
return jwt.sign({id: this._id}, process.env.JWT_SECRET, {expiresIn: process.env.JWT_EXPIRES});
};
userSchema.methods.matchPassword = async function(enteredPassword: string) {
// @ts-ignore
return await bcrypt.compare(enteredPassword, this.password);
};
userSchema.methods.getResetPasswordToken = function() {
const resetToken = crypto.randomBytes(20).toString('hex');
// @ts-ignore
this.resetPasswordToken = crypto.createHash('sha256').update(resetToken).digest('hex');
// @ts-ignore
this.resetPasswordExpire = Date.now() + 10 * 60 * 1000;
return resetToken;
}
export default mongoose.model('User', userSchema);
这是我正在使用的界面:
使用者.ts
import { Document } from 'mongoose';
export default interface User extends Document {
firstName: string,
lastName: string,
email: string,
password: string,
role: string,
resetPasswordToken: string,
resetPasswordExpire: Date
}
我注意到,当我将match属性添加到email对象时,出现了错误。
2条答案
按热度按时间k97glaaz1#
对于mongoose模式,您应该使用
capitalized
版本的String,它是javascript String Object中的实际值类型,不应该与typescript基元字符串类型混淆。2admgd592#
在
getResetPasswordToken
中,在return resetToken
之前添加this.save()