NESTJS从控制器中的空结果中抛出异常
我有一个调用服务的控件。 如果服务从DB返回空的有效载荷,我想提出异常。
目前,我在服务中这样做: 这是我目前提供的服务。
async getPreferences(eUserId: string): Promise<UserPreferences> {
const userPreferences = await this.userPreferencesModel.findOne({
eUserId,
});
if (!userPreferences) {
throw new NotFoundException("We couldn't find your user preferences");
}
return userPreferences;
}
我希望控制器处理异常,问题是控制器响应是一个承诺。 我该如何处理?
这就是我剃须的操作:
@Get()
async getPreferences(
@Headers('x-e-user-id') eUserId: string,
): Promise<UserPreferences> {
const userPreferences = this.userPreferencesService.getPreferences(eUserId);
console.log('userPreferences: ', userPreferences);
// Here is what I am trying to monitor...
if (userPreferences) {
throw new NotFoundException("We couldn't find your user preferences");
}
return userPreferences;
}
控制器返回中的ther console.log:
userPreferences: Promise { <pending> }
现在,如果服务响应为空,则毫无例外。
我如何监视服务结果以进行异常
I have a control that calls a service.
If the service returns an empty payload from the db I want to throw an exception.
at the moment I am doing that in the service:
this is the service I have at the moment with the exception.
async getPreferences(eUserId: string): Promise<UserPreferences> {
const userPreferences = await this.userPreferencesModel.findOne({
eUserId,
});
if (!userPreferences) {
throw new NotFoundException("We couldn't find your user preferences");
}
return userPreferences;
}
I want the controller to handle the exception, The issue is that the controller response is a Promise.
How can I handle that?
This is what I shave done:
@Get()
async getPreferences(
@Headers('x-e-user-id') eUserId: string,
): Promise<UserPreferences> {
const userPreferences = this.userPreferencesService.getPreferences(eUserId);
console.log('userPreferences: ', userPreferences);
// Here is what I am trying to monitor...
if (userPreferences) {
throw new NotFoundException("We couldn't find your user preferences");
}
return userPreferences;
}
Ther console.log in the controller returns:
userPreferences: Promise { <pending> }
Now, if the service response is empty no exception is thrown.
How can I monitor the service result in order to throw an exception
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以解决此问题的多种方法。这是一个。
不要在服务中丢失错误,只需返回结果或
null
。然后,在您的控制器
等待结果
中,您忘记了此。这就是为什么您看到待决的承诺的原因。结果解决后,请检查是否返回任何用户首选项,然后扔notFoundException
(如果不是)。我不会扔
notfoundException
或您服务中的任何其他HTTP相关错误。将该责任留给您的控制器,不要将您的服务(逻辑)与HTTP错误代码联系起来。在这里抛出不知道它们正在使用的上下文(HTTP)的错误。PS:您还可以考虑通过查询字符串或路由参数传递用户ID,而不是通过标头来传递用户ID。
Multiple ways you can solve this. Here's one.
Don't throw an error in your service, just return the result or
null
.Then in your controller
await
for the result, you forgot this. That's why you are seeing a pending promise. After the result has been resolved, check if any user preferences were returned and throw theNotFoundException
if not.I would not throw
NotFoundException
or any other HTTP related error from your service. Leave that responsibility up to your controller, don't tie your service (logic) to HTTP error codes. Throw errors here that are not aware of the context (HTTP) they are being used in.PS: You might also consider passing the user ID via the query string or as a route parameter instead of via the headers.