Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/41.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Node.js TypeScript:执行一个函数,返回express的中间件_Node.js_Typescript_Express - Fatal编程技术网

Node.js TypeScript:执行一个函数,返回express的中间件

Node.js TypeScript:执行一个函数,返回express的中间件,node.js,typescript,express,Node.js,Typescript,Express,我已经创建了一个中间件,它将验证传入的数据。这里的问题是我创建了一个函数,该函数以joi对象为参数,并返回中间件。这将导致以下错误。此外,该错误仅在构建期间发生,并在vscode上显示红色旋转线。在开发过程中,一切正常 No overload matches this call. Overload 1 of 3, '(path: PathParams, ...handlers: RequestHandler<ParamsDictionary, any, any, ParsedQs, R

我已经创建了一个中间件,它将验证传入的数据。这里的问题是我创建了一个函数,该函数以joi对象为参数,并返回中间件。这将导致以下错误。此外,该错误仅在构建期间发生,并在vscode上显示红色旋转线。在开发过程中,一切正常

No overload matches this call.
  Overload 1 of 3, '(path: PathParams, ...handlers: RequestHandler<ParamsDictionary, any, any, ParsedQs, Record<string, any>>[]): Router', gave the following error.
    Argument of type '(req: Request, res: Response, next: NextFunction) => void' is not assignable to parameter of type 'RequestHandler<ParamsDictionary, any, any, ParsedQs, Record<string, any>>'.
      Types of parameters 'req' and 'req' are incompatible.
        Type 'Request<ParamsDictionary, any, any, ParsedQs, Record<string, any>>' is missing the following properties from type 'Request': cache, credentials, destination, integrity, and 15 more.
  Overload 2 of 3, '(path: PathParams, ...handlers: RequestHandlerParams<ParamsDictionary, any, any, ParsedQs, Record<string, any>>[]): Router', gave the following error.
    Argument of type '(req: Request, res: Response, next: NextFunction) => void' is not assignable to parameter of type 'RequestHandlerParams<ParamsDictionary, any, any, ParsedQs, Record<string, any>>'.
      Type '(req: Request, res: Response, next: NextFunction) => void' is not assignable to type 'RequestHandler<ParamsDictionary, any, any, ParsedQs, Record<string, any>>'.ts(2769)
View Problem (Alt+F8)
以及验证

import HttpException from '@exceptions/HttpException';
import { NextFunction } from 'express';
import joi, { ObjectSchema } from 'joi';

const Validate = (schema: ObjectSchema = joi.object({})): ((req: Request, res: Response, next: NextFunction) => void) => {
  return (req: Request, res: Response, next: NextFunction) => {
    const result = schema.validate(req.body);
    if (result.error) {
      next(new HttpException(406, result.error.message.split(`\"`).join('')));
    }
    next();
  };
};

export default Validate;

不从express导入请求和响应是一个愚蠢的错误。如果未导入,则使用fetch api的请求和响应

import HttpException from '@exceptions/HttpException';
import joi, { ObjectSchema } from 'joi';
import { Request, Response, NextFunction } from 'express'; //should be imported

const Validate = (schema: ObjectSchema = joi.object({})): ((req: Request, res: Response, next: NextFunction) => void) => {
  return (req: Request, res: Response, next: NextFunction) => {
    const result = schema.validate(req.body);
    if (result.error) {
      next(new HttpException(406, result.error.message.split(`\"`).join('')));
    }
    next();
  };
};

export default Validate;

这不是对你问题的回答,所以我只留下一条评论:当验证失败时,你应该使用400 HTTP状态码。例如,当客户端要求您以所选格式获取数据(例如
Accept
header中的
application/json
)但您的服务器无法这样做时,使用406。此处的更多信息:您能否显示
初始值设定项outes
中定义/导入/需要
验证
(小写)的位置?您的验证器返回工厂类型方法,但这几乎就像您将架构从Validate传递到返回的函数中一样。还要检查在定义了
Validate
的文件中是否导入了正确的
请求
响应
类型,这可能是问题所在,因为可能存在同名的全局类型。@Joe我已经更新了中间件函数的代码。@arkus u是对的,我没有导入请求和响应。谢谢
import HttpException from '@exceptions/HttpException';
import joi, { ObjectSchema } from 'joi';
import { Request, Response, NextFunction } from 'express'; //should be imported

const Validate = (schema: ObjectSchema = joi.object({})): ((req: Request, res: Response, next: NextFunction) => void) => {
  return (req: Request, res: Response, next: NextFunction) => {
    const result = schema.validate(req.body);
    if (result.error) {
      next(new HttpException(406, result.error.message.split(`\"`).join('')));
    }
    next();
  };
};

export default Validate;