Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix TypeError When Using @Paginate() with @MessagePattern() (@nestjs/microservices) #894

Merged
merged 11 commits into from
Sep 6, 2024

Conversation

AAAAAAAAlan
Copy link
Contributor

Problem

Currently, when the @Paginate() decorator is used alongside the @MessagePattern() decorator within a NestJS microservice, an error is thrown. This occurs because ExecutionContextHost.switchToHttp().getRequest() does not contain a query key in 'rpc' mode, leading to undefined being returned and causing the following TypeError:

TypeError: Cannot read properties of undefined (reading 'searchBy')

This error specifically arises due to the incompatibility of the @Paginate() decorator expecting HTTP request objects in an environment where such objects are not available.

@AAAAAAAAlan AAAAAAAAlan changed the title Fix TypeError When Using @Paginate() with @MessagePattern() in RPC Mode Fix TypeError When Using @Paginate() with @MessagePattern() (@nestjs/microservices) Mar 26, 2024
Copy link

codecov bot commented Mar 26, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 97.85%. Comparing base (bbf3b4a) to head (90d4f6d).
Report is 2 commits behind head on master.

Current head 90d4f6d differs from pull request most recent head abd25d1

Please upload reports for the commit abd25d1 to get more accurate results.

Additional details and impacted files
@@           Coverage Diff           @@
##           master     #894   +/-   ##
=======================================
  Coverage   97.85%   97.85%           
=======================================
  Files          16       16           
  Lines         652      653    +1     
  Branches      190      192    +2     
=======================================
+ Hits          638      639    +1     
  Misses         14       14           

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@Helveg
Copy link
Collaborator

Helveg commented Mar 26, 2024

I'm unfamiliar with RPC, could you explain why request instead of request.query is an appropriate fix?

@AAAAAAAAlan
Copy link
Contributor Author

I'm unfamiliar with RPC, could you explain why request instead of request.query is an appropriate fix?

While using @MessagePattern - request variable contains query parameters in itself and nothing more. Im also unfamiliar with RPC, I did some "console.logging" and found that out.

I'm not really interested in contributing or finding other, better solutions. This fix works perfectly in my project, and I just wanted to highlight the issue for maintainers and fellow gamers

src/decorator.ts Outdated Show resolved Hide resolved
src/decorator.ts Outdated Show resolved Hide resolved
@Helveg Helveg requested a review from ppetzold June 6, 2024 13:18
Copy link
Collaborator

@Helveg Helveg left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Finished OP's PR.

(Sort of) added (untested) WS & RCP support, assuming that their query will be in getData().

@Helveg
Copy link
Collaborator

Helveg commented Jun 8, 2024

@ppetzold could you have a look?

@Helveg
Copy link
Collaborator

Helveg commented Sep 5, 2024

@ppetzold ping :)

Copy link
Owner

@ppetzold ppetzold left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nicely done 🚀

@ppetzold ppetzold merged commit 813e893 into ppetzold:master Sep 6, 2024
3 checks passed
@ppetzold
Copy link
Owner

ppetzold commented Sep 6, 2024

🎉 This PR is included in version 9.1.0 🎉

The release is available on:

Your semantic-release bot 📦🚀

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants