簡體   English   中英

輸入'{類型:任何; 必填:[真,字符串]; 獨特的:真實的; 匹配:(字符串|正則表達式)[]; }' 不可分配給類型 'string'

[英]Type '{ type: any; required: [true, string]; unique: true; match: (string | RegExp)[]; }' is not assignable to type 'string'

我正在使用 nodejs 和 typescript 來構建用戶 model。在我的 mongoose model 中,我試圖將匹配屬性添加到架構的 email 字段,但我不斷收到此 882562059.3588 錯誤

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'.

這是 model 文件:

用戶.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
}

我注意到當我將匹配屬性添加到 email object 時出現錯誤。

For mongoose schemas, you should use capitalized version of String ,which is an actual value type in javascript String Object , and should not be confused with the typescript primitive string type .

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM