Argument of type '(res: Response<any, Record<string, any>>, next: NextFunction) => Promise<void>' is not assignable to parameter of type

huangapple go评论75阅读模式
英文:

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&lt;IUser[]&gt;(&quot;SELECT * FROM users&quot;)
    .then((result) =&gt; {
      const results = result as [RowDataPacket, FieldPacket[]];
      for (let i=0; i &lt; results.length ; i++) users[i] = results[0].value;
      res.send(users);
    })
    .catch(err =&gt; {console.log(err); next();})
  }

And the following is the userRouter file:

router
  .route(&quot;/&quot;)
  .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 &#39;(res: Response&lt;any, Record&lt;string, any&gt;&gt;, next: NextFunction) =&gt; Promise&lt;void&gt;&#39; is not assignable to parameter of type &#39;RequestHandlerParams&lt;ParamsDictionary, any, any, ParsedQs, Record&lt;string, any&gt;&gt;&#39;.
      Type &#39;(res: Response&lt;any, Record&lt;string, any&gt;&gt;, next: NextFunction) =&gt; Promise&lt;void&gt;&#39; is not assignable to type &#39;RequestHandler&lt;ParamsDictionary, any, any, ParsedQs, Record&lt;string, any&gt;&gt;&#39;.
        Types of parameters &#39;res&#39; and &#39;req&#39; are incompatible.
          Type &#39;Request&lt;ParamsDictionary, any, any, ParsedQs, Record&lt;string, any&gt;&gt;&#39; is missing the following properties from type &#39;Response&lt;any, Record&lt;string, any&gt;&gt;&#39;: 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

中间件路由函数的参数顺序是 requestresponsenext

尝试使用:

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) { ... }

huangapple
  • 本文由 发表于 2023年1月5日 15:05:19
  • 转载请务必保留本文链接:https://go.coder-hub.com/75014869.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定