[英]Argument of type 'void' is not assignable to parameter of type 'PathParams'
Created route handler
and while adding in my route
.创建路由handler
并添加我的route
。
import {Application, NextFunction} from 'express';
import {container} from 'tsyringe';
const routeConstantsArray = { };
const constants: any = []
constants.push(routeConstantsArray)
let next : NextFunction;
export const loadConstantsMiddleware = (app:Application) => {
app.locals = constants
next();
};
Route路线
import express from 'express';
import {Application} from 'express';
import cors from 'cors';
// Import Internal Modules
import {loadConstantsMiddleware} from '../core/middleware/constants_middleware';
const startupRouterConfig = (app: Application): Application => {
app.use(cors());
app.use(express.json());
app.use(loadConstantsMiddleware(app));
return app;
};
Error错误
(parameter) app: express.Application
No overload matches this call. (参数) app: express.Application
没有重载匹配这个调用。 The last overload gave the following error:最后一个重载给出了以下错误:
Argument of type 'void' is not assignable to parameter of type 'PathParams'.ts(2769)
index.d.ts(55, 5): The last overload is declared here.
To use the loadConstantsMiddleware
as middleware, you have to return a request handler, like:要将loadConstantsMiddleware
用作中间件,您必须返回一个请求处理程序,例如:
export const loadConstantsMiddleware = (app:Application) => {
app.locals = constants;
return (req, res, next) => {
next();
}
};
But in this scenario, it probably doesn't make sense to use it as a middleware since the constants are scoped to the entire application (versus just the request).但是在这种情况下,将其用作中间件可能没有意义,因为常量的范围是整个应用程序(而不是请求)。
To just add it to the app directly, without middleware, you can do:要直接将其添加到应用程序中,无需中间件,您可以执行以下操作:
// main
app.use(cors());
app.use(express.json());
loadConstantsMiddleware(app);
// load-constants-middleware
export const loadConstantsMiddleware = (app:Application) => {
app.locals = constants
};
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.