类型脚本猫鼬静态模型方法“类型上不存在属性". [英] Typescript mongoose static model method "Property does not exist on type"
问题描述
我目前正在尝试向我的猫鼬模式中添加静态方法,但是我找不到它无法通过这种方式工作的原因.
I am currently trying to add a static method to my mongoose schema but I can't find the reason why it doesn't work this way.
我的模特:
import * as bcrypt from 'bcryptjs';
import { Document, Schema, Model, model } from 'mongoose';
import { IUser } from '../interfaces/IUser';
export interface IUserModel extends IUser, Document {
comparePassword(password: string): boolean;
}
export const userSchema: Schema = new Schema({
email: { type: String, index: { unique: true }, required: true },
name: { type: String, index: { unique: true }, required: true },
password: { type: String, required: true }
});
userSchema.method('comparePassword', function (password: string): boolean {
if (bcrypt.compareSync(password, this.password)) return true;
return false;
});
userSchema.static('hashPassword', (password: string): string => {
return bcrypt.hashSync(password);
});
export const User: Model<IUserModel> = model<IUserModel>('User', userSchema);
export default User;
IUser:
export interface IUser {
email: string;
name: string;
password: string;
}
如果我现在尝试拨打User.hashPassword(password)
,我会收到以下错误[ts] Property 'hashPassword' does not exist on type 'Model<IUserModel>'.
If I now try to call User.hashPassword(password)
I am getting the following error [ts] Property 'hashPassword' does not exist on type 'Model<IUserModel>'.
我知道我没有在任何地方定义方法,但是我真的不知道在哪里可以放置它,因为我不能只是将静态方法放到接口中. 希望您能帮助我找到错误,在此先感谢!
I know that I didn't define the method anywhere but I don't really know where I could put it as I can't just put a static method into an interface. I hope you can help my find the error, thanks in advance!
推荐答案
我认为您遇到了我刚刚遇到的同样问题.您可以拨打此电话.几个教程让您从这样的模型中调用.comparePassword()
方法.
I think you are having the same issue that I just struggled with. This issue is in your call. Several tutorials have you call the .comparePassword()
method from the model like this.
User.comparePassword(candidate, cb...)
这不起作用,因为该方法位于schema
而不是model
上.我能够调用该方法的唯一方法是使用标准的mongoose/mongo查询方法找到该模型的实例.
This doesn't work because the method is is on the schema
not on the model
. The only way I was able to call the method was by finding this instance of the model using the standard mongoose/mongo query methods.
这是我的护照中间件的相关部分:
Here is relevant part of my passport middleware:
passport.use(
new LocalStrategy({
usernameField: 'email'
},
function (email: string, password: string, done: any) {
User.findOne({ email: email }, function (err: Error, user: IUserModel) {
if (err) throw err;
if (!user) return done(null, false, { msg: 'unknown User' });
user.schema.methods.comparePassword(password, user.password, function (error: Error, isMatch: boolean) {
if (error) throw error;
if (!isMatch) return done(null, false, { msg: 'Invalid password' });
else {
console.log('it was a match'); // lost my $HÏT when I saw it
return done(null, user);
}
})
})
})
);
因此,我使用findOne({})
来获取文档实例,然后不得不通过挖掘文档user.schema.methods.comparePassword
So I used findOne({})
to get the document instance and then had to access the schema methods by digging into the schema properties on the document user.schema.methods.comparePassword
我注意到了几个区别:
- 我的是
instance
方法,而您的是static
方法.我相信会有类似的方法访问策略. - 我发现我必须将哈希传递给
comparePassword()
函数.也许这对于静态操作不是必需的,但是我无法访问this.password
- Mine is an
instance
method while yours is astatic
method. I'm confident that there is a similar method access strategy. - I found that I had to pass the hash to the
comparePassword()
function. perhaps this isn't necessary on statics, but I was unable to accessthis.password
这篇关于类型脚本猫鼬静态模型方法“类型上不存在属性".的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!