我使用以下函数从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();
1条答案
按热度按时间vbopmzt11#
中间件路由函数的参数顺序为
request
、response
、next
。尝试: