'No overload matches this call. Error in a NodeJS application written in TypeScript
This is my models/user.ts file:
import mongoose , {Schema , Document , Model} from 'mongoose';
import passportLocalMongoose from 'passport-local-mongoose';
export interface digitalWalletDocument extends Document{
currencyName: string;
value: number;
}
const digitalWalletSchema = new Schema<digitalWalletDocument>({
currencyName : {
type : String
},
value : {
type : Number
}
});
export interface UserDocument extends Document{
username : string;
firstName : string;
lastName : string;
email : string;
telNumber : number;
nationalCode : number;
admin : boolean;
image : string;
wallet : number;
rate : number;
shabaCode :number;
cardNumber : number;
birthDay : string;
status : string;
address : string;
documnetPath : string;
description : string;
purchased : string;
digitalWallet : digitalWalletDocument;
}
export const userSchema = new Schema<UserDocument>({
username : {
type : String,
sparse : true ,
unique : true
},
firstName : {
type: String
},
lastName : {
type : String
},
email : {
type : String,
unique : true
},
telNumber : {
type : Number
},
nationalCode : {
type : Number,
sparse : true,
unique : true
},
admin : {
type:Boolean,
default : false
},
image : {
type : String
},
wallet: {
type : Number,
default : 0
},
rate : {
type : Number,
default : 0
},
shabaCode : {
type : Number
},
cardNumber : {
type : Number
},
birthDay : {
type : String
},
status : {
type : String
},
address : {
type : String
},
documnetPath : {
type : String
},
description : {
type: String
},
purchased : [String],
digitalWallet : [digitalWalletSchema]
}, {timestamps : true});
export interface UserModel extends mongoose.PassportLocalModel<UserDocument>{};
userSchema.plugin(passportLocalMongoose,{usernameField : 'email'});
export default mongoose.model<UserDocument,UserModel>('User' , userSchema);
And the following is my authentication.ts file:
import express, {Request, Response, NextFunction} from 'express';
import mongoose from 'mongoose';
import passport, { authenticate } from 'passport';
import passportLocal from 'passport-local';
const LocalStrategy = passportLocal.Strategy;
import passportJWT from 'passport-jwt';
const passportJWTStrategy = passportJWT.Strategy;
const ExtractJWT = passportJWT.ExtractJwt;
import JWT from 'jsonwebtoken';
import User from './models/users';
import config from './config';
export const local = passport.use(new LocalStrategy(User.authenticate()));
passport.serializeUser(User.serializeUser());
passport.deserializeUser(User.deserializeUser());
export function getToken(user: any) {
return JWT.sign(user, config.secretKey,{ expiresIn : "2d"});
};
const opt = {
secretOrKey : config.secretKey,
jwtFromRequest : ExtractJWT.fromAuthHeaderAsBearerToken()
};
export const jwtAuth = passport.use(new passportJWTStrategy(opt , (payload ,done) => {
User.findOne({_id : payload._id},(err: any , user: typeof User ) => {
if(err){
return done(err,false);
}
else if(user){
return done(null , user);
}
else{
return done(null, false);
}
});
}));
export const verifyUser = passport.authenticate('jwt', {session : false});
export function verifyAdmin(req: Request, res: Response, next: NextFunction) {
if(req.user.admin){
return next();
}
else{
let err = new Error('Only Admin can access to this web page or resources!');
err.status = 403;
return next(err);
}
}
I get the following error message in passport.serializeUser(User.serializeUser()); line of the code:
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: UserDocument, 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 'UserDocument': username, firstName, lastName, email, and 66 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: UserDocument, 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 'UserDocument': username, firstName, lastName, email, and 66 more.ts(2769)
Also another errors occurs at these lines of the code:
if(req.user.admin)
and
err.status = 403;
And the error messages are:
Property 'admin' does not exist on type 'User'.ts(2339)
Property 'status' does not exist on type 'Error'.ts(2339)
I don't know what is the problem and how can I fix it? This code works in JS version of the application but I don't know why gives me this error in TS version?
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
