英文:
Argument of type '(res: Response<any, Record<string, any>>, next: NextFunction) => Promise<void>' is not assignable to parameter of type
问题
以下是翻译好的部分:
我有以下函数来从`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);
但它给我以下错误:
没有重载匹配此调用。
最后一个重载提供了以下错误。
类型为'(res: Response<any, Record<string, any>>, next: NextFunction) => Promise<void>'的参数不可分配给类型'RequestHandlerParams<ParamsDictionary, any, any, ParsedQs, Record<string, any>>'。
类型'(res: Response<any, Record<string, any>>, next: NextFunction) => Promise<void>'不可分配给类型'RequestHandler<ParamsDictionary, any, any, ParsedQs, Record<string, any>>'。
参数'res'和'req'的类型不兼容。
类型'Request<ParamsDictionary, any, any, ParsedQs, Record<string, any>>'缺少类型'Response<any, Record<string, any>>'的以下属性:status、sendStatus、links、send,以及还有55个属性。
这是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();
英文:
I have the following function to read all the users from the mysql:
  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();})
  }
And the following is the userRouter file:
router
  .route("/")
  .get(User.readAll);
But it gives me the following error:
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.
This is the user interface:
export interface IUser extends RowDataPacket {
  id?: number
  email: string
  password: string
  admin: boolean
  created_at: Date
}
Also the database config:
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();
答案1
得分: 1
中间件路由函数的参数顺序是 request、response、next。
尝试使用:
static async readAll(req: Request, res: Response, next: NextFunction) { ... }
英文:
The parameter order for a middleware route function is request, response, next.
Try with:
static async readAll(req: Request, res: Response, next: NextFunction) { ... }
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。


评论