gpt4 book ai didi

node.js - 类型 'password' 上不存在属性 'Document'

转载 作者:行者123 更新时间:2023-12-02 19:33:03 26 4
gpt4 key购买 nike

我有一个合作伙伴架构

export const PartnerSchema: Schema = new Schema({
id: {
type: Number,
require: [true, "EL id es necesario"],
default: 0
},
password :{type:String},

});

并且我尝试在使用同一文件中的此函数将文档保存到数据库之前对架构的密码进行哈希处理

PartnerSchema.pre('save', function(next){

let user = this;

// Make sure not to rehash the password if it is already hashed
if(!user.isModified('password')) return next();

// Generate a salt and use it to hash the user's password
bcrypt.genSalt(10, (err, salt) => {

if(err) return next(err);

bcrypt.hash(user.password, salt, (err, hash) => {

if(err) return next(err);
user.password = hash;
next();

});

});

但是在 bcrypt.hash(user.password, salt, (err, hash) => { 我收到此错误

Property 'password' does not exist on type 'Document'

为什么我收到此错误 id user = this 以及所有属性?

最佳答案

您需要为您的预 Hook 定义接口(interface)

export interface IUser extends mongoose.Document {
password: string
}

PartnerSchema.pre<IUser>('save', function(next){...}

关于node.js - 类型 'password' 上不存在属性 'Document',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61493286/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com