mongoose 类型'{类型:任何;必需:[真,字符串];唯一:true;匹配:(字符串|RegExp)[]; }'无法指派给型别'string'

kmb7vmvb  于 2022-11-13  发布在  Go
关注(0)|答案(2)|浏览(99)

我正在使用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对象时,出现了错误。

k97glaaz

k97glaaz1#

对于mongoose模式,您应该使用capitalized版本的String,它是javascript String Object中的实际值类型,不应该与typescript基元字符串类型混淆。

2admgd59

2admgd592#

getResetPasswordToken中,在return resetToken之前添加this.save()

相关问题