node.js - 没有重载匹配此调用错误(Typescript/Express)
问题描述
我创建了一个控制器:
import { Request, Response } from "express";
export const signIn = async (req: Request, res: Response) => {
try {
await res.send("This is the signIn route");
} catch (err) {
return res.status(400).send(err);
}
};
export default signIn;
然后我将控制器导出到我的路由处理程序中:
import express from "express";
const router = express.Router();
import signIn from "../controller/signin";
router.post("/signIn", signIn);
我的 index.ts 文件:
import express from 'express'
import authRoutes from '../src/routes/auth-routes'
import errorHandler from '../src/middlewares/error-handler'
import {connectDB} from '../src/database/db'
const app = express()
connectDB()
const port = 3005
// @Body parser
app.use(express.json())
// @Routes
app.use('/api/users', authRoutes)
app.use(errorHandler)
// @Listening port
app.listen(port, () => {
console.log(`*********Server listening on port ${port}*******`)
})
但是,我收到了这个错误:
{
"resource": "/C:/Projects/myAppName/auth/src/routes/auth-routes.ts",
"owner": "typescript",
"code": "2769",
"severity": 8,
"message": "No overload matches this call.\n Overload 1 of 3, '(path: PathParams, ...handlers: RequestHandler<ParamsDictionary, any, any, ParsedQs, Record<string, any>>[]): Router', gave the following error.\n Argument of type '(req: Request, res: Response) => Promise<Response<any, Record<string, any>> | undefined>' is not assignable to parameter of type 'RequestHandler<ParamsDictionary, any, any, ParsedQs, Record<string, any>>'.\n Types of parameters 'req' and 'req' are incompatible.\n Type 'Request<ParamsDictionary, any, any, ParsedQs, Record<string, any>>' is missing the following properties from type 'Request<ParamsDictionary, any, any, ParsedQs, Record<string, any>>': RequestValidationError, DatabaseConnectionError\n Overload 2 of 3, '(path: PathParams, ...handlers: RequestHandlerParams<ParamsDictionary, any, any, ParsedQs, Record<string, any>>[]): Router', gave the following error.\n Argument of type '(req: Request, res: Response) => Promise<Response<any, Record<string, any>> | undefined>' is not assignable to parameter of type 'RequestHandlerParams<ParamsDictionary, any, any, ParsedQs, Record<string, any>>'.\n Type '(req: Request, res: Response) => Promise<Response<any, Record<string, any>> | undefined>' is not assignable to type 'RequestHandler<ParamsDictionary, any, any, ParsedQs, Record<string, any>>'.\n Overload 3 of 3, '(path: PathParams, subApplication: Application): Router', gave the following error.\n Argument of type '(req: Request, res: Response) => Promise<Response<any, Record<string, any>> | undefined>' is not assignable to parameter of type 'Application'.\n Type '(req: Request<ParamsDictionary, any, any, ParsedQs, Record<string, any>>, res: Response<any, Record<string, any>>) => Promise<...>' is missing the following properties from type 'Application': init, defaultConfiguration, engine, set, and 61 more.",
"source": "ts",
"startLineNumber": 12,
"startColumn": 24,
"endLineNumber": 12,
"endColumn": 30
}
我不明白为什么我得到这个“没有重载匹配这个调用错误”。请有人可以看看我上面的代码并帮助我吗?我会非常感激的。
解决方案
推荐阅读
- python - 如何在python中以特定字符开头的字符串列表中选择特定数量的元素?
- android - kotlin couroutine async 在 Android 的 viewModelScope 中不起作用
- mqtt - 如何从会话描述中的语义中删除 WMS?
- python - Python 3 中 input() 函数的限制范围
- java - Java:为什么 Calendral.setTimeInMillis(0) 设置不正确的时间
- image-processing - 将 320x240x3 点云矩阵转换为 320x240x1 深度图
- r - 当目标不均匀分布时为训练数据集选择行
- performance - 我看不到 perf 的功率/能量核心选项来测量功耗
- javascript - 当我单击图标时,语义 UI 会在下拉菜单中对下拉菜单做出反应,并关闭搜索
- kubernetes - 如何使 pod 在 EKS 工作节点上的特定路径上运行