Node.js Express中的身份验证。财产';比较密码';不存在于类型';文件';
我不熟悉express和node.js。我正在尝试构建一个没有前端的身份验证系统。我正在使用打字脚本、passport、passport local和mongoose。我得到以下错误:Node.js Express中的身份验证。财产';比较密码';不存在于类型';文件';,node.js,mongodb,typescript,express,mongoose,Node.js,Mongodb,Typescript,Express,Mongoose,我不熟悉express和node.js。我正在尝试构建一个没有前端的身份验证系统。我正在使用打字脚本、passport、passport local和mongoose。我得到以下错误: TSError: ⨯ Unable to compile TypeScript: auth.ts:20:37 - error TS2339: Property 'comparePassword' does not exist on type 'Document'. 20 const password
TSError: ⨯ Unable to compile TypeScript:
auth.ts:20:37 - error TS2339: Property 'comparePassword' does not exist on type 'Document'.
20 const passwordOK = await user.comparePassword(password);
~~~~~~~~~~~~~~~
auth.ts:32:19 - error TS2339: Property '_id' does not exist on type 'Model<Document, {}>'.
32 done(null, user._id);
~~~
这是我的crmModel.ts代码
import mongoose from "mongoose";
import * as EmailValidator from "email-validator";
import { NextFunction } from "express";
import { Document } from "mongoose";
const bcrypt = require("bcrypt");
const SALT_ROUNDS = 12;
interface props extends Document {
username: string;
email: string;
password: string;
}
export const UserSchema = new mongoose.Schema(
{
username: {
type: String,
required: true,
trim: true,
index: { unique: true },
minlength: 3,
},
email: {
type: String,
required: true,
trim: true,
lowercase: true,
index: { unique: true },
validate: {
validator: EmailValidator.validate,
message: (props) => `${props.value} is not valid email address!`,
},
},
password: {
type: String,
required: true,
trim: true,
index: { unique: true },
minlength: 8,
},
},
{
timestamps: true,
}
).pre<props>("save", async function (next) {
const user = this;
if (!user.isModified("password")) return next();
try {
const hash = await bcrypt.hash(user.password, SALT_ROUNDS);
user.password = hash;
return next();
} catch (err) {
return next(err);
}
});
UserSchema.methods.comparePassword = async function comparePassword(
candidate: string
) {
return bcrypt.compare(candidate, this.password);
};
您能指导我吗?TypeScript无法从您的架构定义中获取类型,因此您必须手动描述它们。修改
crmModel
文件:
import { Document, Model } from "mongoose";
export interface UserDocument extends Document {
username: string;
email: string;
password: string;
comparePassword(candidate: string): Promise<boolean> // or whatever it returns
}
export interface UserModel extends Model<UserDocument> {}
const UserSchema = new mongoose.Schema<UserDocument>({
// ...
}).pre<UserDocument>('save', async function(/* ... */) {
// ...
})
UserSchema.methods.comparePassword = async function(/* */) { /* ... */}
const User = mongoose.model<UserDocument, UserModel>('User', UserSchema)
export default User
从“mongoose”导入{Document,Model};
导出接口UserDocument扩展文档{
用户名:字符串;
电子邮件:字符串;
密码:字符串;
comparePassword(候选者:string):Promise//或它返回的任何内容
}
导出接口UserModel扩展模型{}
const UserSchema=new mongoose.Schema({
// ...
}).pre('save',异步函数(/*…*/){
// ...
})
UserSchema.methods.comparePassword=异步函数(/***/){/*…*/}
const User=mongoose.model('User',UserSchema)
导出默认用户
请注意,我在crmModel
文件中移动了模型声明,您可以稍后在任何地方导入它,模型应该创建一次。您还可以在UserDocument
中添加所需的任何其他方法,并在UserModel
中添加静态方法
虽然我刚刚意识到,我不确定为什么它说findOne不是一个函数,因为它应该在任何模型上定义,但我希望添加接口可以解决这个问题
findOne is not a function
import { Document, Model } from "mongoose";
export interface UserDocument extends Document {
username: string;
email: string;
password: string;
comparePassword(candidate: string): Promise<boolean> // or whatever it returns
}
export interface UserModel extends Model<UserDocument> {}
const UserSchema = new mongoose.Schema<UserDocument>({
// ...
}).pre<UserDocument>('save', async function(/* ... */) {
// ...
})
UserSchema.methods.comparePassword = async function(/* */) { /* ... */}
const User = mongoose.model<UserDocument, UserModel>('User', UserSchema)
export default User