如何将异步处理程序函数传递到路由

问题描述 投票:0回答:1
app.get()

方法中使用异步函数。当我这样做时,我会遇到错误。 我的路由器:

   import { Router } from "express";
   import { register } from "../controllers/auth";
   const authRouter = Router();

   authRouter.post("/register", register);

   export default authRouter;
错误:

No overload matches this call. The last overload gave the following error. Argument of type '(req: Request<ParamsDictionary, any, any, ParsedQs, Record<string, any>>, res: Response<any, Record<string, any>>, next: NextFunction) => Promise<...>' is not assignable to parameter of type 'Application<Record<string, any>>'. Type '(req: Request<ParamsDictionary, any, any, ParsedQs, Record<string, any>>, res: Response<any, Record<string, any>>, next: NextFunction) => Promise<...>' is missing the following properties from type 'Application<Record<string, any>>': init, defaultConfiguration, engine, set, and 63 more.ts(2769) index.d.ts(168, 5): The last overload is declared here.

我找到了一种通过将我的异步处理程序包裹在正常功能中来管理它的方法,但我不确定这是否是正确的方法:

authRouter.post("/register", (req, res, next) => { register(req, res, next); });

在我的内存中,我永远不会添加问题,将异步方法作为我的路线或中间件的处理程序。
    

您可以尝试一下;

   import { Router } from "express";
   import { register } from "../controllers/auth.js";
   export const authRouter = Router();

   authRouter.post("/register", register);

I在您的控制器导入中添加了“ .js”,我使用导出const而是导出默认表达式。但是我可以看到您的索引。

javascript node.js typescript express asynchronous
1个回答
0
投票
最新问题
© www.soinside.com 2019 - 2025. All rights reserved.