P
Prisma•4mo ago
Mohamed Lamine

Why methods of a service not working when $use is used?

Hey Prisma community! I'm facing a weird issue when trying to consume any methods of my service which is based on prisma service/client to make operations. For example, when I tried to get all event sessions I got undefined (which I consider as a weird issue/behavior). I'm using NestJs w/ Prisma (as you can see in the attached image). I'm aware of I should use Prisma client extensions because middlewares are deprecated: the reason of described behavior (when commented, methods are working). But, it would be great if I have a deeper explanation.
No description
3 Replies
RaphaelEtim
RaphaelEtim•4mo ago
Hi @Mohamed Lamine 👋 In your middleware logic, you specified
params.model === 'EventSession' && params.action === 'create'
params.model === 'EventSession' && params.action === 'create'
This logic only runs for for create action and EventSession model. Since the middleware is only for create, it should not impact findMany. You can add logs just before the if statement in your middleware to see the action attached to params
this.prisma.$use(async (params, next) => {
console.log('Middleware invoked with params:', params);
if (params.model === 'EventSession' && params.action === 'create') {
this.prisma.$use(async (params, next) => {
console.log('Middleware invoked with params:', params);
if (params.model === 'EventSession' && params.action === 'create') {
Mohamed Lamine
Mohamed LamineOP•4mo ago
Hello @RaphaelEtim thanks for your reply. It seems the (bad) way I implemented it make that this middleware ($use) is going to run every single time I do prisma.whatever.whatever i.e anytime I do a query. So it's normal for findMany to be impacted by the middleware. So I add the following line of code out of the if statement: return next(params); . This ensure that others queries e.g findMany proceed normally when conditions are not met/satisfied.
Mohamed Lamine
Mohamed LamineOP•4mo ago
I added log as you suggested. Here it's the result:
No description
Want results from more Discord servers?
Add your server