处理所有承诺和其他内容时,请获得请求不返回数据,仍会发送

发布于 2025-01-27 17:47:17 字数 1210 浏览 1 评论 0原文

我正在尝试查询数据中的一些数据。但是,当我将获取请求发送到服务器时,它不会返回任何内容。但是,当我主机记录数据时,我的数据将在控制台上打印。我处理所有承诺和其他东西。但是结果是相同的。

控制器方法: -

  @Get('order/:state')
  async getOrderByStatus(
    @Res() response: Response,
    @Param('state') state: number,
  ): Promise<OrderService[]> {
    try {
      const orderStatus = await this.orderServiceService.getOrderByStatus(
        state,
      );
      if (orderStatus) {
        console.log(orderStatus);
        return orderStatus;
      }
    } catch (error) {
      console.log(error);
      response.status(401).send(error);
    }
  }

服务方法: -

  async getOrderByStatus(state: number): Promise<OrderService[]> {
    try {
      const orderState = await this.orderModel.find({ orderStatus: state });
      if (orderState) {
        return orderState;
      }
      throw new HttpException('Order not found', HttpStatus.NOT_FOUND);
    } catch (error) {
      console.log(error);
      throw error;
    }
  }

客户端: - (请求数据仍在发送。

我真的需要您的帮助...谢谢....❤️

I am trying to query some data in my data. But when I send get request to my server, It's not returning anything. But when I console log my data is printed on the console. I handle all promises and other stuff. But the results are the same.

Controller method: -

  @Get('order/:state')
  async getOrderByStatus(
    @Res() response: Response,
    @Param('state') state: number,
  ): Promise<OrderService[]> {
    try {
      const orderStatus = await this.orderServiceService.getOrderByStatus(
        state,
      );
      if (orderStatus) {
        console.log(orderStatus);
        return orderStatus;
      }
    } catch (error) {
      console.log(error);
      response.status(401).send(error);
    }
  }

Service method: -

  async getOrderByStatus(state: number): Promise<OrderService[]> {
    try {
      const orderState = await this.orderModel.find({ orderStatus: state });
      if (orderState) {
        return orderState;
      }
      throw new HttpException('Order not found', HttpStatus.NOT_FOUND);
    } catch (error) {
      console.log(error);
      throw error;
    }
  }

Client-side:- (Request data is still sending.Not returning data from my controller)
enter image description here

I really need your help... Thank you..❤️

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

我爱人 2025-02-03 17:47:17

当您将@Res()添加到路由处理程序时,您告诉NEST您要使用特定图书馆的方法您会自己处理发送响应。如果您不需要任何基础引擎特定的方法,我建议不要在路由处理程序中使用@Res(),并且如果您确实需要一些方法,但仍然希望Nest发送响应您最终可以使用@Res({passthrough:true}),只需确保您自己最终不会自己调用res.send自己即可。

对于处理错误,您可以捕获错误并将其重新输入为httpexception,这样,Nest的内置异常过滤器将为您发送适当的响应代码和消息

When you add @Res() to the route handler, you are telling Nest that you want to use the library-specific approach and you'll handle sending the response on your own. If you don't need any underlying engine specific methods, I'd suggest not using @Res() in the route handler, and if you do need some method, but still want Nest to send the response you can end up using @Res({ passthrough: true }), just make sure that you don't end up calling res.send yourself.

For handling the error, you can catch the error and re-throw it as an HttpException, that way Nest's built-in exception filter will send the proper response code and message for you

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文