处理所有承诺和其他内容时,请获得请求不返回数据,仍会发送
我正在尝试查询数据中的一些数据。但是,当我将获取请求发送到服务器时,它不会返回任何内容。但是,当我主机记录数据时,我的数据将在控制台上打印。我处理所有承诺和其他东西。但是结果是相同的。
控制器方法: -
@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)
I really need your help... Thank you..❤️
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
当您将
@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 callingres.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