typescript 类型的参数(res:响应〈任意,记录〈字符串,任意>>,下一个:NextFunction)=>Promise< void>“不能赋值给类型为的参数

ubof19bj  于 2023-01-06  发布在  TypeScript
关注(0)|答案(1)|浏览(360)

我使用以下函数从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();
vbopmzt1

vbopmzt11#

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

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

相关问题