【发布时间】:2021-08-15 22:51:35
【问题描述】:
我正在尝试使用 Passportjs 和 mongoose 进行身份验证,但我很难使用 typescript 获得正确的类型。
Passport.use(UserModel.createStrategy())
Passport.serializeUser(UserModel.serializeUser()) // <---- Error
我得到错误:
No overload matches this call.
Overload 1 of 2, '(fn: (user: User, done: (err: any, id?: any) => void) => void): void', gave the following error.
Argument of type '(user: PassportLocalModel<User>, cb: (err: any, id?: any) => void) => void' is not assignable to parameter of type '(user: User, done: (err: any, id?: any) => void) => void'.
Types of parameters 'user' and 'user' are incompatible.
Type 'User' is missing the following properties from type 'PassportLocalModel<User>': authenticate, serializeUser, deserializeUser, register, and 68 more.
Overload 2 of 2, '(fn: (req: IncomingMessage, user: User, done: (err: any, id?: unknown) => void) => void): void', gave the following error.
Argument of type '(user: PassportLocalModel<User>, cb: (err: any, id?: any) => void) => void' is not assignable to parameter of type '(req: IncomingMessage, user: User, done: (err: any, id?: unknown) => void) => void'.
Types of parameters 'user' and 'req' are incompatible.
Type 'IncomingMessage' is missing the following properties from type 'PassportLocalModel<User>': authenticate, serializeUser, deserializeUser, register, and 53 more.
这就是我的 User 类的样子:
export interface User extends Document {
email: String
password: String
displayName: String
}
const UserSchema = new Schema(
{
email: { type: String, unique: true },
password: String,
displayName: String,
},
{ timestamps: true }
)
UserSchema.plugin(PassportLocalMongoose, {
usernameField: 'email',
})
export const UserModel = model('User', UserSchema)
【问题讨论】:
标签: node.js typescript mongoose passport.js passport-local-mongoose