[V5] how to get values from ManyToMany relationship on the Middleware? #2190
Answered
by
McSneaky
vicainelli
asked this question in
Help
-
Hey all, I'm writing a test for an API and a facing this problem when I trying to put a middleware to check if the User can access the Establishment // app/Models/User.ts
@manyToMany(() => Establishment)
public establishments: ManyToMany<typeof Establishment> But I got // app/Middleware/EstablishmentUser.ts
public async handle({ request, response, auth }: HttpContextContract, next: () => Promise<void>) {
const userEstablishments = await auth.user?.establishments
// do something to check if user cannot access
if (check) return response.status(401)
await next()
}
And works well when I fetch from the endpoint using |
Beta Was this translation helpful? Give feedback.
Answered by
McSneaky
Feb 2, 2021
Replies: 1 comment 2 replies
-
// await auth.user.preload('establishments') (deprecated)
await auth.user.load('establishments')
const userEstablishments = auth.user.establishments It works if you preload it first in the middleware |
Beta Was this translation helpful? Give feedback.
2 replies
Answer selected by
vicainelli
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
It works if you preload it first in the middleware