I have a Users
class like following:
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;
}
}
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:
[
{
"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"
}
]
Why this happens and how can I fix it?
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("SELECT * FROM users" )
To:
const rows : any = await Pools.execute("SELECT * FROM users" )