类型 '(res: Response<any, Record<string, any>>, next: NextFunction) => Promise<void



我有以下函数从mysql中读取所有users:

static async readAll(res:Response , next: NextFunction){
let user : IUser;
let users : [IUser];
await pool.query<IUser[]>("SELECT * FROM users")
.then((result) => {
const results = result as [RowDataPacket, FieldPacket[]];
for (let i=0; i < results.length ; i++) users[i] = results[0].value;
res.send(users);
})
.catch(err => {console.log(err); next();})
}

userRouter文件如下:

router
.route("/")
.get(User.readAll);

但是它给了我以下错误:

No overload matches this call.
The last overload gave the following error.
Argument of type '(res: Response<any, Record<string, any>>, next: NextFunction) => Promise<void>' is not assignable to parameter of type 'RequestHandlerParams<ParamsDictionary, any, any, ParsedQs, Record<string, any>>'.
Type '(res: Response<any, Record<string, any>>, next: NextFunction) => Promise<void>' is not assignable to type 'RequestHandler<ParamsDictionary, any, any, ParsedQs, Record<string, any>>'.
Types of parameters 'res' and 'req' are incompatible.
Type 'Request<ParamsDictionary, any, any, ParsedQs, Record<string, any>>' is missing the following properties from type 'Response<any, Record<string, any>>': status, sendStatus, links, send, and 55 more.

这是user接口:

export interface IUser extends RowDataPacket {
id?: number
email: string
password: string
admin: boolean
created_at: Date
}

还有数据库配置:

const pool =  mysql.createPool(
{
host : (process.env.DB_HOST),
user : (process.env.DB_USER),
password: (process.env.DB_PASSWORD ),
database: (process.env.DB_NAME ),
waitForConnections: true,
connectionLimit: 10,
queueLimit: 0
}).promise();

中间件路由函数参数顺序为:requestresponsenext

试一试:

static async readAll(req: Request, res: Response, next: NextFunction) { ... }

最新更新