【问题标题】:The controller does not have time to process requests and ValidationPipe skips them控制器没有时间处理请求,ValidationPipe 跳过它们
【发布时间】:2022-11-28 01:58:42
【问题描述】:

我有一个表格来添加新项目。当我多次点击添加按钮时,它会发送很多请求。 ValidationPipe 第一次检查名称是否唯一,并跳过后续请求,因为控制器还没有时间处理第一个请求。(添加延迟 2 秒)。结果,它创建了重复的项目。

@Post()
  @UsePipes(new ValidationPipe({
    forbidUnknownValues: true,
    exceptionFactory
  }))
  @UseGuards(JwtAuthGuard)
  async create(@Body() createItemDto: CreateItemDto, @Req() req: Request) {
    const token = req.cookies['at'];

    const { id } = await this.authService.verifyJwtToken(token);
    
    const item = Object.assign(createItemDto, {
      userId: id
    })

    await new Promise(r => setTimeout(r, 2000));

    return await this.itemsService.create(item);
  }

【问题讨论】:

  • 嗨 ptimer!到底是什么问题?代码中间超时 2 秒的原因是什么?这将导致您编写脚本(并可能导致服务器开始阻塞/内存不足(取决于您启动控制器的方式

标签: node.js reactjs nestjs class-validator


【解决方案1】:

如果我的理解正确,您可以在处理第一个请求之前多次发布?您应该创建一个队列,一次处理一个创建端点的创建请求(最好是每个 userId)。您可以在这里阅读有关队列的更多信息:(https://docs.nestjs.com/techniques/queues)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-01-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多