Type 'void | RowDataPacket[] | RowDataPacket[][] | OkPacket | OkPacket[] | ResultSetHeader' is not assignable to type 'IUser[]'

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

Type 'void | RowDataPacket[] | RowDataPacket[][] | OkPacket | OkPacket[] | ResultSetHeader' is not assignable to type 'IUser[]'

问题

我有一个名为Users的类,如下所示:

class Users {

  static async findAll(req: Request, res: Response, next: NextFunction): Promise<IUser[]> {
    const rows = await Pools.execute("SELECT * FROM users")
      .then((rows) => {
        return rows[0];
      })
      .catch(err => console.log(err));
    next();
    return rows;

  }
}

但是我在最后一行return rows;处遇到了错误:

类型 'void | RowDataPacket[] | RowDataPacket[][] | OkPacket | OkPacket[] | ResultSetHeader' 不能分配给类型 'IUser[]'。
类型 'void' 不能分配给类型 'IUser[]'。

这是IUser接口的声明:

import { RowDataPacket } from "mysql2"

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

export { IUser };

我知道rows[0]的结构如下:

[
  {
    "id": 1,
    "email": "admin@u.com",
    "password": "12345",
    "username": "admin@u.com",
    "admin": 1,
    "created_at": "2023-01-06T02:31:14.000Z"
  },
  {
    "id": 2,
    "email": "u@u.com",
    "password": "12345",
    "username": "u@u.com",
    "admin": 0,
    "created_at": "2023-01-06T03:08:20.000Z"
  }
]

为什么会发生这种情况,我该如何修复它?

英文:

I have a Users class like following:

class Users {

  static async findAll(req: Request, res: Response, next: NextFunction) : Promise&lt;IUser[]&gt; {
    const rows = await Pools.execute(&quot;SELECT * FROM users&quot; )
      .then((rows) =&gt; {
      return rows[0];
      })
      .catch(err =&gt; console.log(err));
    next();
    return rows;

  }
}

But I get the error at the last line return rows; :

> Type 'void | RowDataPacket[] | RowDataPacket[][] | OkPacket |
> OkPacket[] | ResultSetHeader' is not assignable to type 'IUser[]'.
> Type 'void' is not assignable to type 'IUser[]'.

This is the declaration of IUser interface:

import { RowDataPacket } from "mysql2"

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

export {IUser};

I know the rows[0] is like the following:

[
  {
    &quot;id&quot;: 1,
    &quot;email&quot;: &quot;admin@u.com&quot;,
    &quot;password&quot;: &quot;12345&quot;,
    &quot;username&quot;: &quot;admin@u.com&quot;,
    &quot;admin&quot;: 1,
    &quot;created_at&quot;: &quot;2023-01-06T02:31:14.000Z&quot;
  },
  {
    &quot;id&quot;: 2,
    &quot;email&quot;: &quot;u@u.com&quot;,
    &quot;password&quot;: &quot;12345&quot;,
    &quot;username&quot;: &quot;u@u.com&quot;,
    &quot;admin&quot;: 0,
    &quot;created_at&quot;: &quot;2023-01-06T03:08:20.000Z&quot;
  }
]

Why this happens and how can I fix it?

答案1

得分: 0

我不确定是否找到了正确的解决方案,但通过更改以下行,错误将消失:

const rows = await Pools.execute("SELECT * FROM users")

到:

const rows: any = await Pools.execute("SELECT * FROM users")
英文:

I am not sure if I found the correct solution but the error will gone by changing the following line:

const rows = await Pools.execute(&quot;SELECT * FROM users&quot; )

To:

const rows : any = await Pools.execute(&quot;SELECT * FROM users&quot; )

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

发表评论

匿名网友

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

确定