Home > database >  Argument of type '(res: Response<any, Record<string, any>>, next: NextFunction) =&g
Argument of type '(res: Response<any, Record<string, any>>, next: NextFunction) =&g

Time:01-06

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();

CodePudding user response:

The parameter order for a middleware route function is request, response, next.

Try with:

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